home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / quintus / quintus0.lha / work / rewrite_2.06.2 < prev    next >
Text File  |  1992-04-03  |  96KB  |  2,819 lines

  1. %%%
  2. %%% version 2.00.1
  3. %%%   initial version
  4. %%% version 2.00.2
  5. %%%   fixed rewriting of eqaulity literals
  6. %%% version 2.00.3 
  7. %%%   changed "equality_rewriting" to "auto_orient"
  8. %%%   added "outrwr"
  9. %%%   added "outrwt"
  10. %%% version 2.00.4
  11. %%%   fixed priority of rewritten term
  12. %%% version 2.00.5
  13. %%%   added recurive path ordering
  14. %%% version 2.00.6
  15. %%%   added user specified rewrite rules and orderings
  16. %%% version 2.00.7
  17. %%%   moved term rewriting to hypermatching
  18. %%% version 2.00.8
  19. %%%   added lexicographic path ordering
  20. %%% version 2.00.9
  21. %%%   added bound on term rewriting
  22. %%%   moved ground from rewrite to library
  23. %%%   moved unground from rewrite to library
  24. %%%   simplified rewriting of equations
  25. %%%   optimized assert_rewrite_rule
  26. %%%   optimized generate_eq_rewrite_rules
  27. %%% version 2.01.0
  28. %%%   checked clause priority after each hyper-link
  29. %%%   fixed bug in rewrite_clauses which asserted duplicate clauses
  30. %%% version 2.01.1
  31. %%%   modified rewrite_lit so that only arguments of s in not s=t are rewritten
  32. %%% version 2.01.2
  33. %%%   added code to rewrite rewrite rule
  34. %%%   fixed bug in delete_rewrite_rule which sometimes deleted the wrong rule
  35. %%%   modified rewrite_lit so that only arguments of s in s=t are rewritten
  36. %%% version 2.01.3
  37. %%%   moved generation of rewrite rules to hyper-linking
  38. %%%   moved rewriting of input clauses to interpret
  39. %%%   fixed bug when rewriting hyper-links from a unit nucleus
  40. %%%   added code to orient equations
  41. %%%   modified term_order_lpo and term_order_rpo so that order can instantiated
  42. %%%     when calling; that is, let term_order be a test
  43. %%% version 2.01.4
  44. %%%   modified rewriting of equations according to David Plaisted's method
  45. %%%   modified assert_rewrite_rule so that it does not assert a rewrite rule
  46. %%%     if it is an instance of an existing rewrite rule
  47. %%%   fixed method of rewriting rewrite rules so that deleted rewrites rules
  48. %%%     aren't generated again
  49. %%% version 2.01.5
  50. %%%   added code to rewrite asserted clauses
  51. %%%   fixed bug in rewrite_asserted_clauses which caused rewritten clause to
  52. %%%     have clause size and number of non-grounded literals swapped
  53. %%%   fixed bug in rewrite_asserted_clauses which caused duplicate clauses to
  54. %%%     be asserted
  55. %%% version 2.01.8
  56. %%%   modified rewriting of equations so that equations are fully rewritten
  57. %%%   removed use of semi_internal_form from rewrite_asserted_clauses
  58. %%% version 2.01.9
  59. %%%   optimized term rewriting
  60. %%% version 2.02.1
  61. %%%   asserted over_priohm if priority exceeded during term rewriting
  62. %%%   added partial rewrite count
  63. %%%   fixed bug in rewrite_asserted_clauses which caused simplified clauses to
  64. %%%     be printed twice
  65. %%% version 2.02.2
  66. %%%   deleted pulled out form when rewriting asserted clause
  67. %%% version 2.02.6
  68. %%%   optimized lpo_order and rop_order by treating identical terms as
  69. %%%     equivalent
  70. %%% version 2.02.7
  71. %%%   asserted equations corresponding to rewrite rules after simplifying
  72. %%%     rewrite rules
  73. %%% version 2.03.0
  74. %%%   moved rewrite rule simplification to hyper-linking
  75. %%%   set user support flag correctly when asserting equations corresponding to
  76. %%%     rewrite rules
  77. %%%   fixed bug in lexicographical path ordering
  78. %%% version 2.03.2
  79. %%%   modified rewriting of equations so that equations are fully rewritten
  80. %%% version 2.03.4
  81. %%%   modified deleting rewrite rule message
  82. %%%   modified rewrite_clause, rewrite_clause_np, rewrite_lit, rewrite_term,
  83. %%%     and rewrite_term_args_only so that they return immediately if there are
  84. %%%     no rewrite rules
  85. %%% version 2.03.5
  86. %%%   assert equations for rewrite rules only if there are rewrite rules
  87. %%%   removed counters
  88. %%% version 2.03.7
  89. %%%   optimized order_term by saving order of subterms
  90. %%% version 2.04.2
  91. %%%   modified rewriting of equations so that the corresponding rewrite rule
  92. %%%     is not used
  93. %%% version 2.04.3
  94. %%%   erased corresponding equation and pulled out form when simplifying 
  95. %%%     rewrite rule
  96. %%% version 2.04.4
  97. %%%   no longer pull out equations at the beginning of rounds
  98. %%%   used restrictive method for rewriting equations
  99. %%%   erased equation associated with rewrite rule only if equation's priority
  100. %%%     exceeded priority bound
  101. %%%   rewrote s fully before attempting to assert a rewrite rule for s=t
  102. %%%   modified rewriting of s=t with s>t to handle case where rewriting s once
  103. %%%     at outer level generates a term smaller than the normal form of t
  104. %%% version 2.04.5
  105. %%%   added restricted_rewrite to control whether restricted or unrestricted
  106. %%%     rewriting of equations is used
  107. %%%   fixed rewrite_asserted_clauses so it erases pulled out forms of rewritten
  108. %%%     clauses
  109. %%%   fixed rewrite_asserted_clauses so it pulls out rewritten input clauses
  110. %%%   generated rewrite rules in rewrite_asserted_clauses
  111. %%% version 2.04.6
  112. %%%   added fixed_priority
  113. %%% version 2.04.7
  114. %%%   when rewriting asserted clauses generate rewrite rules for equation in
  115. %%%     normal form
  116. %%%   added limited rewrite to hyper-linking
  117. %%%   moved code to erase clause corresponding to rewrite rule from
  118. %%%     erase_rewrite_rule to rmrwr_prio
  119. %%% version 2.04.8
  120. %%%   fixed bug in rewrite_outer which prevented priority test from being done
  121. %%% version 2.04.9
  122. %%%   don't assert a rewrite rule if its lhs or rhs has a priority greater
  123. %%%     than the priority bound
  124. %%%   don't assert a clause whose priority is larger than the priority
  125. %%%   sorted clauses by priority before rewriting
  126. %%% version 2.05.2
  127. %%%   fixed assert_rewrite_rule_equations so that it works when neither
  128. %%%     fixed_priority nor slidepriority is specified
  129. %%% version 2.05.3
  130. %%%   added support for quintus prolog
  131. %%%   don't erase rewrite rules corresponding to input equations
  132. %%%   fixed clause rewriting to run when auto_orient and no rewrite rules
  133. %%% version 2.05.4
  134. %%%   don't erase or assert pulled out forms when rewriting clauses
  135. %%%   erase rewrite rules corresponding to input equations
  136. %%%   when rewriting clauses, erase old clause before checking priority of new
  137. %%%     clause
  138. %%%   when rewriting clauses, don't reject new clause if priority bound not set
  139. %%% version 2.05.5
  140. %%%   added equational rewriting
  141. %%%   fixed a couple of bugs which prevented equational rewriting from working
  142. %%%   don't check priority when input clause rewritten
  143. %%%   fixed bug when rewriting input clause
  144. %%% version 2.05.7
  145. %%%   fixed minor equational rewrite bug in rewrite_lit_6
  146. %%% version 2.05.8
  147. %%%   improved the efficiency of order_term_lpo
  148. %%%   don't assert equational rewrite rule if rhs contains variable not
  149. %%%     contained in lhs
  150. %%% version 2.05.9
  151. %%%   fixed bug in lpo term ordering
  152. %%%   added implementation improvement to lpo term ordering
  153. %%%   fixed bug in rewrite_lit
  154. %%%   fixed bug in rewrite_grounded_term_list
  155. %%% version 2.06.0
  156. %%%   fixed print_rewrite so that the used a consistent naming of variables for
  157. %%%     the original and new clause
  158. %%%   fixed bug in rewrite_outer_equational
  159. %%%   fixed assert rewrite rule to print equational rules in both directions
  160. %%%   fixed bug in restricted rewriting
  161. %%%   added early check for unit conflict
  162. %%% version 2.06.1
  163. %%%   added constrained rewriting
  164. %%%   don't print fully rewritten equations and inequation under restricted
  165. %%%     rewriting
  166. %%%   added save_unrestricted_normal_form
  167. %%%   added early unit conflict check to rewrite_rewrite_rules
  168. %%%   rewrite replace rules
  169. %%% version 2.06.2
  170. %%%   added precompute_constraints
  171. %%%   modified rewrite_lit_with_constraints so that restricted rewriting
  172. %%%     doesn't depend on constraints
  173. %%%   modified priority test for non-constrained clause rewriting to check
  174. %%%     priority of rewritten clause rather than rhs of the rewrite rule
  175. %%%   cached lexicographical path ordering
  176. %%%   added index and size bound to cached_rewrite
  177. %%%   added size bound to cached_constraint_consistent
  178. %%%   added size bound to cached_constrained_term_order and
  179. %%%     cached_constrained_term_order_complete
  180. %%%   don't add to constraint when constraint precomputed
  181. %%%   cached constrained subterm rewrites
  182. %%%   removed rpo code as it isn't supported (or used)
  183. %%%   added cache_size
  184. %%%   fixed assert_rewrite_rules_equations/0 to assert equations for equational
  185. %%%     rewrite rules
  186. %%%   added additional time overflow tests
  187. %%%
  188. %%% This is the code for rewriting clauses.  It also contains code for
  189. %%% generating rewrite rules from equations.
  190. %%%
  191.  
  192. %%%
  193. %%% Rewrite_rule_equations ensures that equations are asserted for each rewrite
  194. %%% rule.
  195. %%%
  196.     rewrite_rule_equations :-
  197.       not(rwr(_,_,_,_)),
  198.       !.
  199.     rewrite_rule_equations :-
  200.       cputime(TT1),
  201.       assert_rewrite_rule_equations,
  202.       cputime(TT2),
  203.       TT3 is TT2 - TT1,
  204.       write_line(5,'Rewrite rule equations(s): ',TT3),
  205.       !.
  206.  
  207. %%%
  208. %%% Clause_rewriting performs clause rewriting.
  209. %%%
  210.     clause_rewriting :-
  211.       not(auto_orient),
  212.       not(rwr(_,_,_,_)),
  213.       !.
  214.     clause_rewriting :-
  215.       cputime(TT1),
  216.       rewrite_asserted_clauses,
  217.       (rewrite_replace_rules_needed -> (
  218.         abolish(rewrite_replace_rules_needed,0),
  219.         rewrite_replace_rules
  220.       ); true),
  221.       cputime(TT2),
  222.       TT3 is TT2 - TT1,
  223.       write_line(5,'Clause rewriting(s): ',TT3),
  224.       !.
  225.  
  226. %%%
  227. %%% Rewrite_asserted_clauses rewriting asserted clauses.
  228. %%%
  229.     testit.
  230.     rewrite_asserted_clauses :-
  231.       ((session_no(3),round_no(1)) -> (
  232.         testit
  233.       ); true),
  234.       bagof1(Pr-Cref,{X1,X2,X3,X4,X5,X6,X7,X8,X9a,X9b,X9c,X9d}^
  235.         clause(sent_C(cl(X1,X2,X3,X4,X5,X6,X7,X8,pr(X9a,X9b,X9c,X9d,Pr))),
  236.         true,Cref),Crefs),
  237.       keysort(Crefs,Crefs1),
  238.       abolish(rac_rwr_generated,0),
  239.       rewrite_asserted_clauses_1(Crefs1),
  240.       ((not(prove_result(_)),not(time_overflow)) -> (
  241.         (rac_rwr_generated -> (
  242.           abolish(rac_rwr_generated,0),
  243.           assert_once(rewrite_replace_rules_needed),
  244.           rewrite_rewrite_rules(1),
  245.           ((not(prove_reuslt(_)),not(time_overflow)) -> (
  246.             rewrite_asserted_clauses
  247.           ); true)
  248.         ); true)
  249.       ); true).
  250.     rewrite_asserted_clauses_1([]) :- !.
  251.     rewrite_asserted_clauses_1([_-Cref|Crefs]) :-
  252.       constrained_rewriting,
  253.       restricted_rewrite,
  254.       !,
  255.       clause(sent_C(cl(_,_,by(C,V1,V1,V,_),Input,Sp,Ds,Rep,Dis,_)),true,Cref),
  256.       rewrite_clause_with_constraint(C,Sp,Ds,[],Cts1),
  257.       ((Cts1\==[[C,0,[]]], Cts1\==[[C,1,[]]]) -> (
  258.     print_rewrite_with_constraint(C,Cts1),
  259.         rewrite_asserted_clauses_2(Cts1,Cs1),
  260.         identical_delete_all_duplicates(Cs1,Cs2),
  261.         identical_delete_all(C,Cs2,Cs3),
  262.         (Cs2==Cs3 -> (
  263.           erase(Cref)
  264.         ); true),
  265.         rewrite_asserted_clauses_3(Cs3,Input,Sp,Ds,Rep,Dis)
  266.       ); true),
  267.       abolish(restricted_rewrite,0),
  268.       rewrite_clause_with_constraint(C,Sp,Ds,[],Cts2),
  269.       assert(restricted_rewrite),
  270.       ((Cts2\==[[C,0,[]]], Cts2\==[[C,1,[]]]) -> (
  271.         rewrite_asserted_clauses_2(Cts2,Cs4),
  272.         identical_delete_all_duplicates(Cs4,Cs5),
  273.         identical_delete_all(C,Cs5,Cs6),
  274.         (Cs5\==Cs6 -> (
  275.           generate_rewrite_rule(C,F),
  276.           (F==1 -> (
  277.             assert_once(rac_rwr_generated)
  278.           ); true)
  279.         ); true),
  280.         rewrite_asserted_clauses_4(Cs6,Input,Sp,Ds,Rep,Dis)
  281.       ); (
  282.         generate_rewrite_rule(C,F),
  283.           (F==1 -> (
  284.             assert_once(rac_rwr_generated)
  285.           ); true)
  286.       )),
  287.       ((not(prove_result(_)), not(time_overflow)) -> (
  288.         not(not(rewrite_asserted_clauses_1(Crefs)))
  289.       ); true).
  290.     rewrite_asserted_clauses_1([_-Cref|Crefs]) :-
  291.       restricted_rewrite,
  292.       !,
  293.       clause(sent_C(cl(_,_,by(C1,V1,V1,V,_),Input,Sp,Ds,Rep,Dis,_)),true,Cref),
  294.       rewrite_clause(C1,Sp,Ds,0,C2,F1),
  295.       (C2\==C1 -> (
  296.         erase(Cref),
  297.         (F1==0 -> (
  298.           rewrite_asserted_clauses_5(C2,Input,Sp,Ds,Rep,Dis)
  299.         ); true)
  300.       ); true),
  301.       ((save_unrestricted_normal_form; C1=[_=_]; C1=not(_=_)) -> (
  302.         abolish(restricted_rewrite,0),
  303.         rewrite_clause_np(C1,Sp,Ds,0,C3,F2),
  304.         assert(restricted_rewrite),
  305.         (F2==0 -> (
  306.           generate_rewrite_rule(C3,F3),
  307.           (F3==1 -> (
  308.             assert_once(rac_rwr_generated)
  309.           ); true),
  310.           (save_unrestricted_normal_form -> (
  311.             rewrite_asserted_clauses_5(C3,Input,Sp,Ds,Rep,Dis)
  312.           ); ( 
  313.             rewrite_asserted_clauses_8(C3)
  314.           ))
  315.         ); true)
  316.       ); true),
  317.       ((not(prove_result(_)), not(time_overflow)) -> (
  318.         not(not(rewrite_asserted_clauses_1(Crefs)))
  319.       ); true).
  320.     rewrite_asserted_clauses_1([_-Cref|Crefs]) :-
  321.       constrained_rewriting,
  322.       !,
  323.       clause(sent_C(cl(_,_,by(C,V1,V1,_,_),Input,Sp,Ds,Rep,Dis,_)),true,Cref),
  324.       rewrite_clause_with_constraint(C,Sp,Ds,[],Cts),
  325.       ((Cts\==[[C,0,[]]], Cts\==[[C,1,[]]]) -> (
  326.         rewrite_asserted_clauses_2(Cts,Cs1),
  327.         identical_delete_all_duplicates(Cs1,Cs2),
  328.         identical_delete_all(C,Cs2,Cs3),
  329.         (Cs2==Cs3 -> (
  330.           erase(Cref)
  331.         ); (
  332.           generate_rewrite_rule(C,F),
  333.           (F==1 -> (
  334.             assert_once(rac_rwr_generated)
  335.           ); true)
  336.         )),
  337.         rewrite_asserted_clauses_4(Cs3,Input,Sp,Ds,Rep,Dis)
  338.       ); true),
  339.       ((not(prove_result(_)), not(time_overflow)) -> (
  340.         not(not(rewrite_asserted_clauses_1(Crefs)))
  341.       ); true).
  342.     rewrite_asserted_clauses_1([_-Cref|Crefs]) :-
  343.       clause(sent_C(cl(_,_,by(C1,V1,V1,_,_),Input,Sp,Ds,Rep,Dis,_)),true,Cref),
  344.       rewrite_clause(C1,Sp,Ds,0,C2,_),
  345.       (C2\==C1 -> (
  346.          erase(Cref),
  347.          rewrite_asserted_clauses_5(C2,Input,Sp,Ds,Rep,Dis)
  348.       ); true),
  349.       generate_rewrite_rule(C2,F),
  350.       (F==1 -> (
  351.         assert_once(rac_rwr_generated)
  352.       ); true),
  353.       ((not(prove_result(_)), not(time_overflow)) -> (
  354.         not(not(rewrite_asserted_clauses_1(Crefs)))
  355.       ); true).
  356.     rewrite_asserted_clauses_2([],[]) :- !.
  357.     rewrite_asserted_clauses_2([[C,_,_]|Cts],[C|Cs]) :-
  358.       rewrite_asserted_clauses_2(Cts,Cs).
  359.     rewrite_asserted_clauses_3([],_,_,_,_,_) :- !.
  360.     rewrite_asserted_clauses_3([C|Cs],Input,Sp,Ds,Rep,Dis) :-
  361.       rewrite_asserted_clauses_5(C,Input,Sp,Ds,Rep,Dis),
  362.       rewrite_asserted_clauses_3(Cs,Input,Sp,Ds,Rep,Dis).
  363.     rewrite_asserted_clauses_4([],_,_,_,_,_) :- !.
  364.     rewrite_asserted_clauses_4([C|Cs],Input,Sp,Ds,Rep,Dis) :-
  365.       generate_rewrite_rule(C,F),
  366.       (F==1 -> (
  367.         assert_once(rac_rwr_generated)
  368.       ); true),
  369.       ((restricted_rewrite, save_unrestricted_normal_form) -> (
  370.         rewrite_asserted_clauses_5(C,Input,Sp,Ds,Rep,Dis)
  371.       ); true),
  372.       rewrite_asserted_clauses_4(Cs,Input,Sp,Ds,Rep,Dis).
  373.     rewrite_asserted_clauses_5(C,Input,Sp,Ds,Rep,Dis) :-
  374.       calculate_priority_clause(C,Sp,Ds,Pr),
  375.       rewrite_asserted_clauses_6(C,Sp,Ds,Pr,Input),
  376.       rewrite_asserted_clauses_7(C,Input,Sp,Ds,Rep,Dis,Pr),
  377.       !.
  378.     rewrite_asserted_clauses_5(_,_,_,_,_,_).
  379.     rewrite_asserted_clauses_6(C2,Sp,Ds,Pr,0) :-
  380.       priority_bound(PrioBound),
  381.       !,
  382.       check_prioritybound(Pr,PrioBound),
  383.       !.
  384.     rewrite_asserted_clauses_6(_,_,_,_,_).
  385.     rewrite_asserted_clauses_7(C2,Input,Sp,Ds,Rep,Dis,Pr) :-
  386.       delete_all_instances,
  387.       !,
  388.       vars_tail(C2,V),
  389.       not(instance_clause_C(C2,V)),
  390.       rewrite_asserted_clauses_8(C2),
  391.       linearize_term(C2,C,V1,V2),
  392.       clause_size(C2,CSize),
  393.       vars_literals(C2,W),
  394.       compute_V_lits(W,0,NLits),
  395.       assertz(sent_C(cl(CSize,NLits,by(C,V1,V2,V,W),Input,Sp,Ds,Rep,Dis,Pr))),
  396.       !.
  397.     rewrite_asserted_clauses_7(C2,Input,Sp,Ds,Rep,Dis,Pr) :-
  398.       linearize_term(C2,C,V1,V2),
  399.       vars_tail(C2,V),
  400.       clause_size(C2,CSize),
  401.       not(duplicate_clause_C(CSize,C2,V)),
  402.       rewrite_asserted_clauses_8(C2),
  403.       vars_literals(C2,W),
  404.       compute_V_lits(W,0,NLits),
  405.       assertz(sent_C(cl(CSize,NLits,by(C,V1,V2,V,W),Input,Sp,Ds,Rep,Dis,Pr))),
  406.       !.
  407.     rewrite_asserted_clauses_8([U]) :-
  408.       check_unit_conflict_C(U),
  409.       !,
  410.       assert_tryresult('unsatisfiable'),
  411.       assert_prooftype('UC').
  412.     rewrite_asserted_clauses_8(_).
  413.  
  414. %%%
  415. %%% Rewrite_clause rewrites a clause.
  416. %%%
  417.     rewrite_clause(C,_,_,_,C,0) :-
  418.       not(rwr(_,_,_,_)),
  419.       !.
  420.     rewrite_clause(C1,Sp,Ds,0,C2,F) :-
  421.       term_size(C1,Size),
  422.       vars_tail(C1,V),
  423.       (restricted_rewrite -> (
  424.         ((cached_restricted_clause_rewrite(Size,C1,var(V1,V2,V3),C2),
  425.             not(not(const_list(V))),
  426.             not(not(const_list(V3))),
  427.             not(not((V1=V2,const_list(V))))) -> (
  428.           assert(clause_rewrite_found)
  429.         ); true)
  430.       ); (
  431.         ((cached_nonrestricted_clause_rewrite(Size,C1,var(V1,V2,V3),C2),
  432.             not(not(const_list(V))),
  433.             not(not(const_list(V3))),
  434.             not(not((V1=V2,const_list(V))))) -> (
  435.           assert(clause_rewrite_found)
  436.         ); true)
  437.       )),
  438.       (clause_rewrite_found -> (
  439.         abolish(clause_rewrite_found,0)
  440.       ); (
  441.         (var(Sp) -> (
  442.           Sp1=sp(0,0,0),
  443.           Ds1=ds(0,0,0)
  444.         ); (
  445.           Sp1=Sp,
  446.           Ds1=Ds
  447.         )),
  448.         calculate_priority_clause(C1,Sp1,Ds1,pr(_,_,_,_,Pr)),
  449.         assert(saved_clause_size(Pr)),
  450.         rewrite_clause_1(C1,Sp1,Ds1,0,C2,F),
  451.         abolish(saved_clause_size,1),
  452.         counter(cached_clause_rewrite_count,N),
  453.         cache_size(CacheSize),
  454.         (N > CacheSize -> (
  455.           write_line(0,'*** cleared cached_clause_rewrite: ',N),
  456.           set_counter(cached_clause_rewrite_count,0),
  457.           abolish(cached_restricted_clause_rewrite,4),
  458.           abolish(cached_nonrestricted_clause_rewrite,4)
  459.         ); true),
  460.         increment_counter(cached_clause_rewrite_count,Size),
  461.         linearize_term(C1,C3,V4,V5),
  462.         (restricted_rewrite -> (
  463.           assert(cached_restricted_clause_rewrite(Size,C3,var(V4,V5,V),C2))
  464.         ); (
  465.           assert(cached_nonrestricted_clause_rewrite(Size,C3,var(V4,V5,V),C2))
  466.         )),
  467.         print_rewrite(C1,C2)
  468.       )).
  469.     rewrite_clause(C1,_,_,1,C2,F) :-
  470.       rewrite_clause_1(C1,_,_,1,C2,F),
  471.       print_rewrite(C1,C2).
  472.     rewrite_clause_1([],_,_,_,[],0) :- !.
  473.     rewrite_clause_1([L1|Ls1],Sp,Ds,Lim,[L2|Ls2],F) :-
  474.       rewrite_lit(L1,Sp,Ds,Lim,L2,F1),
  475.       rewrite_clause_2(Ls1,Sp,Ds,Lim,Ls2,F,F1).
  476.     rewrite_clause_2(Ls1,Sp,Ds,Lim,Ls2,F,0) :-
  477.       rewrite_clause_1(Ls1,Sp,Ds,Lim,Ls2,F).
  478.     rewrite_clause_2(Ls1,Sp,Ds,Lim,Ls2,1,1) :-
  479.       rewrite_clause_1(Ls1,Sp,Ds,Lim,Ls2,_).
  480.  
  481. %%%
  482. %%% Rewrite_clause_np rewrites a clause without printing the rewrite even if
  483. %%% outrwt is asserted.
  484. %%%
  485.     rewrite_clause_np(C1,Sp,Ds,Lim,C2,F) :-
  486.       (outrwt -> (
  487.         abolish(outrwt,0),
  488.         rewrite_clause(C1,Sp,Ds,Lim,C2,F),
  489.         assert(outrwt)
  490.       ); (
  491.         rewrite_clause(C1,Sp,Ds,Lim,C2,F)
  492.       )),
  493.       !.
  494.  
  495. %%%
  496. %%% Rewrite_lit rewrites a literal.
  497. %%%
  498.     rewrite_lit(S=T,Sp,Ds,Lim,L,F) :-
  499.       restricted_rewrite,
  500.       !,
  501.       order_term(S,O,T),
  502.       rewrite_lit_2(S,O,T,Sp,Ds,Lim,L,F),
  503.       !.
  504.     rewrite_lit(S=T,Sp,Ds,Lim,L,F) :-
  505.       !,
  506.       remove_rewrite_rule(S=T),
  507.       rewrite_term(S,Sp,Ds,Lim,S1,F1),
  508.       restore_rewrite_rule(S=T),
  509.       remove_rewrite_rule(T=S),
  510.       rewrite_term(T,Sp,Ds,Lim,T1,F2),
  511.       restore_rewrite_rule(T=S),
  512.       orient_equation(S1=T1,L),
  513.       rewrite_lit_1(F1,F2,F),
  514.       !.
  515.     rewrite_lit(not S1=T1,Sp,Ds,Lim,not S2=T2,F) :-
  516.       restricted_rewrite,
  517.       !,
  518.       rewrite_term_args_only(S1,Sp,Ds,Lim,S2,F1),
  519.       rewrite_term(T1,Sp,Ds,Lim,T2,F2),
  520.       rewrite_lit_1(F1,F2,F),
  521.       !.
  522.     rewrite_lit(not L1,Sp,Ds,Lim,not L2,F) :-
  523.       rewrite_term(L1,Sp,Ds,Lim,L2,F),
  524.       !.
  525.     rewrite_lit(L1,Sp,Ds,Lim,L2,F) :-
  526.       rewrite_term(L1,Sp,Ds,Lim,L2,F),
  527.       !.
  528.     rewrite_lit_1(1,_,1) :- !.
  529.     rewrite_lit_1(_,1,1) :- !.
  530.     rewrite_lit_1(_,_,0) :- !.
  531.     rewrite_lit_2(S,>,T,Sp,Ds,Lim,L,F) :-
  532.       !,
  533.       rewrite_term(T,Sp,Ds,Lim,T1,F1),
  534.       rewrite_lit_4(S,Sp,Ds,Lim,T1,L,F1,F),
  535.       !.
  536.     rewrite_lit_2(S,<,T,Sp,Ds,Lim,L,F) :-
  537.       !,
  538.       rewrite_term(S,Sp,Ds,Lim,S1,F1),
  539.       rewrite_lit_4(T,Sp,Ds,Lim,S1,L,F1,F),
  540.       !.
  541.     rewrite_lit_2(S1,_,T1,Sp,Ds,Lim,S2=T2,F) :-
  542.       rewrite_term(S1,Sp,Ds,Lim,S2,F1),
  543.       rewrite_term(T1,Sp,Ds,Lim,T2,F2),
  544.       rewrite_lit_1(F1,F2,F),
  545.       !.
  546.     rewrite_lit_3(S1,S2,T2,_,_,_,S2=T2,0) :-
  547.       S1==S2,
  548.       !.
  549.     rewrite_lit_3(_,S2,T2,Sp,Ds,Lim,L,F) :-
  550.       rewrite_term(S2,Sp,Ds,Lim,S3,F),
  551.       orient_equation(S3=T2,L),
  552.       !.
  553.     rewrite_lit_4(S1,Sp,Ds,Lim,T2,T2=S3,F1,F) :-
  554.       vars_tail(S1,V),
  555.       rewrite_lit_5(Lim,S1,S2,V),
  556.       order_term(S2,<,T2),
  557.       !,
  558.       rewrite_term(S2,Sp,Ds,Lim,S3,F2),
  559.       rewrite_lit_1(F1,F2,F),
  560.       !.
  561.     rewrite_lit_4(S1,Sp,Ds,Lim,T2,L,F1,F) :-
  562.       rewrite_term_args_only(S1,Sp,Ds,Lim,S2,F2),
  563.       rewrite_lit_3(S1,S2,T2,Sp,Ds,Lim,L,F3),
  564.       rewrite_lit_1(F1,F2,F4),
  565.       rewrite_lit_1(F4,F3,F),
  566.       !.
  567.     rewrite_lit_5(0,S1,S2,V) :-
  568.       rwr((S1->S2),var(V1,V2,_),_,_),
  569.       unify_lists(V1,V2),
  570.       not(not(const_list(V))).
  571.     rewrite_lit_5(0,S1,S2,V) :-
  572.       equational_rewrite,
  573.       rwr((S1=S2),var(V1,V2,_),_,_),
  574.       unify_lists(V1,V2),
  575.       not(not(const_list(V))),
  576.       order_term(S1,>,S2).
  577.     rewrite_lit_5(1,S1,S2,V) :-
  578.       rwr((S1->S2),var(V1,V2,_),0,_),
  579.       unify_lists(V1,V2),
  580.       not(not(const_list(V))).
  581.  
  582. %%%
  583. %%% Rewrite_term_args_only rewrites the arguments of a term.
  584. %%%
  585.     rewrite_term_args_only(T1,Sp,Ds,Lim,T2,F1) :-
  586.       ground(T1,T3,Vs,Cs),
  587.       functor(T3,F,N),
  588.       rewrite_grounded_arguments(N,T3,Sp,Ds,Vs,Cs,Lim,As2,F1),
  589.       T4=..[F|As2],
  590.       unground(T4,T2,Vs,Cs),
  591.       !.
  592.  
  593. %%%
  594. %%% Rewrite_grounded_arguments rewrites the arguments of a grounded term.
  595. %%%
  596.     rewrite_grounded_arguments(0,_,_,_,_,_,_,[],0) :- !.
  597.     rewrite_grounded_arguments(N,T,Sp,Ds,Vs,Cs,0,[T2|Ts2],F1) :-
  598.       equational_rewrite,
  599.       !,
  600.       functor(T,_,N1),
  601.       N2 is N1-N+1,
  602.       arg(N2,T,T1),
  603.       rewrite_term_1(T1,Sp,Ds,Vs,Cs,0,T3,F2),
  604.       rewrite_term_5(T3,Sp,Ds,Vs,Cs,0,T4,F2,F3),
  605.       rewrite_term_9(T3,T4,Sp,Ds,Vs,Cs,0,T2,F3,F),
  606.       N3 is N-1,
  607.       rewrite_grounded_arguments_1(N3,T,Sp,Ds,Vs,Cs,0,F,Ts2,F1),
  608.       !.
  609.     rewrite_grounded_arguments(N,T,Sp,Ds,Vs,Cs,Lim,[T2|Ts2],F1) :-
  610.       functor(T,_,N1),
  611.       N2 is N1-N+1,
  612.       arg(N2,T,T1),
  613.       rewrite_term_1(T1,Sp,Ds,Vs,Cs,Lim,T2,F),
  614.       N3 is N-1,
  615.       rewrite_grounded_arguments_1(N3,T,Sp,Ds,Vs,Cs,Lim,F,Ts2,F1),
  616.       !.
  617.     rewrite_grounded_arguments_1(0,_,_,_,_,_,_,1,[],1) :- !.
  618.     rewrite_grounded_arguments_1(N,T,_,_,_,_,_,1,[Ti|Ts],1) :-
  619.       !,
  620.       functor(T,_,N1),
  621.       N2 is N1-N+1,
  622.       arg(N2,T,Ti),
  623.       N3 is N-1,
  624.       rewrite_grounded_arguments_1(N3,T,_,_,_,_,_,1,Ts,1).
  625.     rewrite_grounded_arguments_1(N,T,Sp,Ds,Vs,Cs,Lim,_,Ts2,F) :-
  626.       !,
  627.       rewrite_grounded_arguments(N,T,Sp,Ds,Vs,Cs,Lim,Ts2,F).
  628.  
  629. %%%      
  630. %%% Rewrite_term reduces a term to its normal form.
  631. %%%
  632.     rewrite_term(T1,Sp,Ds,0,T2,F) :-
  633.       equational_rewrite,
  634.       !,
  635.       ground(T1,T3,Vs,Cs),
  636.       rewrite_term_1(T3,Sp,Ds,Vs,Cs,0,T4,F1),
  637.       rewrite_term_5(T4,Sp,Ds,Vs,Cs,0,T5,F1,F2),
  638.       rewrite_term_9(T4,T5,Sp,Ds,Vs,Cs,0,T6,F2,F),
  639.       unground(T6,T2,Vs,Cs),
  640.       !.
  641.     rewrite_term(T1,Sp,Ds,Lim,T2,F) :-
  642.       ground(T1,T3,Vs,Cs),
  643.       rewrite_term_1(T3,Sp,Ds,Vs,Cs,Lim,T4,F),
  644.       unground(T4,T2,Vs,Cs),
  645.       !.
  646.     rewrite_term_1(T1,Sp,Ds,Vs,Cs,Lim,T2,F1) :-
  647.       rewrite_outer(T1,Sp,Ds,Vs,Cs,Lim,T3,F),
  648.       rewrite_term_2(T3,Sp,Ds,Vs,Cs,Lim,F,T2,F1),
  649.       !.
  650.     rewrite_term_2(T,_,_,_,_,_,1,T,1) :- !.
  651.     rewrite_term_2(T1,Sp,Ds,Vs,Cs,Lim,_,T2,F1) :-
  652.       rewrite_args(T1,Sp,Ds,Vs,Cs,Lim,T3,F),
  653.       !,
  654.       rewrite_term_3(T1,T3,Sp,Ds,Vs,Cs,Lim,F,T2,F1),
  655.       !.
  656.     rewrite_term_3(_,T,_,_,_,_,_,1,T,1) :- !.
  657.     rewrite_term_3(T1,T2,_,_,_,_,_,_,T2,0) :-
  658.       T1==T2,
  659.       !.
  660.     rewrite_term_3(_,T1,Sp,Ds,Vs,Cs,Lim,_,T2,F1) :-
  661.       rewrite_outer(T1,Sp,Ds,Vs,Cs,Lim,T3,F),
  662.       !,
  663.       rewrite_term_4(T1,T3,Sp,Ds,Vs,Cs,Lim,F,T2,F1),
  664.       !.
  665.     rewrite_term_4(_,T,_,_,_,_,_,1,T,1) :- !.
  666.     rewrite_term_4(T1,T2,_,_,_,_,_,_,T2,0) :-
  667.       T1==T2,
  668.       !.
  669.     rewrite_term_4(_,T1,Sp,Ds,Vs,Cs,Lim,_,T2,F1) :-
  670.       rewrite_args(T1,Sp,Ds,Vs,Cs,Lim,T3,F),
  671.       !,
  672.       rewrite_term_3(T1,T3,Sp,Ds,Vs,Cs,Lim,F,T2,F1),
  673.       !.
  674.     rewrite_term_5(T,_,_,_,_,_,T,1,1) :- !.
  675.     rewrite_term_5(T1,Sp,Ds,Vs,Cs,Lim,T2,0,F1) :-
  676.       rewrite_outer_equational(T1,Sp,Ds,Vs,Cs,Lim,T3,F),
  677.       rewrite_term_6(T3,Sp,Ds,Vs,Cs,Lim,F,T2,F1),
  678.       !.
  679.     rewrite_term_6(T,_,_,_,_,_,1,T,1) :- !.
  680.     rewrite_term_6(T1,Sp,Ds,Vs,Cs,Lim,_,T2,F1) :-
  681.       rewrite_args_equational(T1,Sp,Ds,Vs,Cs,Lim,T3,F),
  682.       !,
  683.       rewrite_term_7(T1,T3,Sp,Ds,Vs,Cs,Lim,F,T2,F1),
  684.       !.
  685.     rewrite_term_7(_,T,_,_,_,_,_,1,T,1) :- !.
  686.     rewrite_term_7(T1,T2,_,_,_,_,_,_,T2,0) :-
  687.       T1==T2,
  688.       !.
  689.     rewrite_term_7(_,T1,Sp,Ds,Vs,Cs,Lim,_,T2,F1) :-
  690.       rewrite_outer_equational(T1,Sp,Ds,Vs,Cs,Lim,T3,F),
  691.       !,
  692.       rewrite_term_8(T1,T3,Sp,Ds,Vs,Cs,Lim,F,T2,F1),
  693.       !.
  694.     rewrite_term_8(_,T,_,_,_,_,_,1,T,1) :- !.
  695.     rewrite_term_8(T1,T2,_,_,_,_,_,_,T2,0) :-
  696.       T1==T2,
  697.       !.
  698.     rewrite_term_8(_,T1,Sp,Ds,Vs,Cs,Lim,_,T2,F1) :-
  699.       rewrite_args_equational(T1,Sp,Ds,Vs,Cs,Lim,T3,F),
  700.       !,
  701.       rewrite_term_7(T1,T3,Sp,Ds,Vs,Cs,Lim,F,T2,F1),
  702.       !.
  703.     rewrite_term_9(_,T,_,_,_,_,_,T,1,1) :- !.
  704.     rewrite_term_9(T1,T2,_,_,_,_,_,T2,_,0) :-
  705.       T1==T2,
  706.       !.
  707.     rewrite_term_9(_,T1,Sp,Ds,Vs,Cs,Lim,T2,_,F) :-
  708.       rewrite_term_1(T1,Sp,Ds,Vs,Cs,Lim,T3,F1),
  709.       rewrite_term_10(T1,T3,Sp,Ds,Vs,Cs,Lim,T2,F1,F),
  710.       !.
  711.     rewrite_term_10(_,T,_,_,_,_,_,T,1,1) :- !.
  712.     rewrite_term_10(T1,T2,_,_,_,_,_,T2,_,0) :-
  713.       T1==T2,
  714.       !.
  715.     rewrite_term_10(_,T1,Sp,Ds,Vs,Cs,Lim,T2,_,F) :-
  716.       rewrite_term_5(T1,Sp,Ds,Vs,Cs,Lim,T3,0,F1),
  717.       rewrite_term_9(T1,T3,Sp,Ds,Vs,Cs,Lim,T2,F1,F),
  718.       !.
  719.  
  720. %%%
  721. %%% Rewrite_outer fully reduces a term at the outer most level.
  722. %%%
  723.     rewrite_outer(T1,_,_,_,_,Lim,T2,0) :-
  724.       not(fixed_priority),
  725.       not(slidepriority),
  726.       !,
  727.       rewrite_outer_1(T1,T2,Lim).
  728.     rewrite_outer(T1,Sp,Ds,Vs,Cs,0,T2,F1) :-
  729.       rewrite_outer_2(T1,Sp,Ds,Vs,Cs,T2,F1),
  730.       !.
  731.     rewrite_outer(T1,_,_,_,_,1,T2,0) :-
  732.       rewrite_outer_1(T1,T2,1),
  733.       !.
  734.     rewrite_outer_1(T1,T2,0) :-
  735.       rwr((T1->T3),var(V1,V1,_),_,_),
  736.       const_list(V),
  737.       not(dont_use_rewrite_rule_outer(T1=T3,V)),
  738.       !,
  739.       rewrite_outer_1(T3,T2,0).
  740.     rewrite_outer_1(T1,T2,1) :-
  741.       rwr((T1->T3),var(V1,V1,_),0,_),
  742.       const_list(V),
  743.       not(dont_use_rewrite_rule_outer(T1=T3,V)),
  744.       !,
  745.       rewrite_outer_1(T3,T2,1).
  746.     rewrite_outer_1(T,T,_).
  747.     rewrite_outer_2(T1,Sp,Ds,Vs,Cs,T2,F1) :-
  748.       rwr((T1->T3),var(V1,V1,_),_,_),
  749.       const_list(V),
  750.       not(dont_use_rewrite_rule_outer(T1=T3,V)),
  751.       !,
  752.       rewrite_outer_3(T1,T3,Sp,Ds,Vs,Cs,T2,F1).
  753.     rewrite_outer_2(T,_,_,_,_,T,0).
  754.     rewrite_outer_3(T1,T2,Sp,Ds,Vs,Cs,T2,1) :-
  755.       nonvar(Sp),
  756.       priority_bound(PrioBound),
  757.       unground(T1,T3,Vs,Cs),
  758.       unground(T2,T4,Vs,Cs),
  759.       calculate_priority_clause([T3],Sp,Ds,pr(_,_,_,_,Pr1)),
  760.       calculate_priority_clause([T4],Sp,Ds,pr(_,_,_,_,Pr2)),
  761.       (retract(saved_clause_size(Pr3)) -> (
  762.         Pr is Pr3 - Pr1 + Pr2,
  763.         assert(saved_clause_size(Pr))
  764.       ); (
  765.         Pr is  Pr2
  766.       )),
  767.       not(check_prioritybound(pr(_,_,_,_,Pr),PrioBound)),
  768.       !.
  769.     rewrite_outer_3(_,T3,Sp,Ds,Vs,Cs,T2,F1) :-
  770.       !,
  771.       rewrite_outer_2(T3,Sp,Ds,Vs,Cs,T2,F1).
  772.  
  773. %%%
  774. %%% Rewrite_outer_equational fully reduces a term at the outer most level using
  775. %%% equational rewrite rules.
  776. %%%
  777.     rewrite_outer_equational(T,_,_,_,_,_,T,0) :-
  778.       not(equational_rewrite),
  779.       !.
  780.     rewrite_outer_equational(T1,_,_,Vs,Cs,Lim,T2,0) :-
  781.       not(fixed_priority),
  782.       not(slidepriority),
  783.       !,
  784.       rewrite_outer_equational_1(T1,T2,Vs,Cs,Lim).
  785.     rewrite_outer_equational(T1,Sp,Ds,Vs,Cs,0,T2,F1) :-
  786.       rewrite_outer_equational_2(T1,Sp,Ds,Vs,Cs,T2,F1),
  787.       !.
  788.     rewrite_outer_equational(T1,_,_,Vs,Cs,1,T2,0) :-
  789.       rewrite_outer_equational_1(T1,T2,Vs,Cs,1),
  790.       !.
  791.     rewrite_outer_equational_1(T1,T2,Vs,Cs,0) :-
  792.       rwr(T1=T3,var(V1,V1,_),_,_),
  793.       unground({T1,T3},{T4,T5},Vs,Cs),
  794.       order_term(T4,>,T5),
  795.       !,
  796.       rewrite_outer_equational_1(T3,T2,Vs,Cs,0).
  797.     rewrite_outer_equational_1(T1,T2,Vs,Cs,1) :-
  798.       rwr(T1=T3,var(V1,V1,_),0,_),
  799.       unground({T1,T3},{T4,T5},Vs,Cs),
  800.       order_term(T4,>,T5),
  801.       !,
  802.       rewrite_outer_equational_1(T3,T2,Vs,Cs,1).
  803.     rewrite_outer_equational_1(T,T,_,_,_).
  804.     rewrite_outer_equational_2(T1,Sp,Ds,Vs,Cs,T2,F1) :-
  805.       rwr(T1=T3,var(V1,V1,_),_,_),
  806.       unground({T1,T3},{T4,T5},Vs,Cs),
  807.       order_term(T4,>,T5),
  808.       !,
  809.       rewrite_outer_equational_3(T1,T3,T5,Sp,Ds,Vs,Cs,T2,F1).
  810.     rewrite_outer_equational_2(T,_,_,_,_,T,0).
  811.     rewrite_outer_equational_3(T1,T2,T3,Sp,Ds,_,_,T2,1) :-
  812.       nonvar(Sp),
  813.       priority_bound(PrioBound),
  814.       calculate_priority_clause([T1],Sp,Ds,pr(_,_,_,_,Pr1)),
  815.       calculate_priority_clause([T3],Sp,Ds,pr(_,_,_,_,Pr2)),
  816.       (retract(saved_clause_size(Pr3)) -> (
  817.         Pr is Pr3 - Pr1 + Pr2,
  818.         assert(saved_clause_size(Pr))
  819.       ); (
  820.         Pr is Pr2
  821.       )),
  822.       not(check_prioritybound(pr(_,_,_,_,Pr),PrioBound)),
  823.       !.
  824.     rewrite_outer_equational_3(_,T3,_,Sp,Ds,Vs,Cs,T2,F1) :-
  825.       !,
  826.       rewrite_outer_equational_2(T3,Sp,Ds,Vs,Cs,T2,F1).
  827.  
  828. %%%
  829. %%% Rewrite_args fully reduces the arguments of a term.
  830. %%%
  831.     rewrite_args(T1,Sp,Ds,Vs,Cs,Lim,T2,F1) :-
  832.       functor(T1,F,N),
  833.       rewrite_args_1(N,T1,Sp,Ds,Vs,Cs,Lim,As,F1),
  834.       T2=..[F|As],
  835.       !.
  836.     rewrite_args_1(0,_,_,_,_,_,_,[],0) :- !.
  837.     rewrite_args_1(N,T,Sp,Ds,Vs,Cs,Lim,[A|As],F1) :-
  838.       functor(T,_,N1),
  839.       N2 is N1-N+1,
  840.       arg(N2,T,A1),
  841.       rewrite_term_1(A1,Sp,Ds,Vs,Cs,Lim,A,F),
  842.       N3 is N-1,
  843.       rewrite_args_2(N3,T,Sp,Ds,Vs,Cs,Lim,F,As,F1),
  844.       !.
  845.     rewrite_args_2(0,_,_,_,_,_,_,1,[],1) :- !.
  846.     rewrite_args_2(N,T,_,_,_,_,Lim,1,[A|As],F) :-
  847.       functor(T,_,N1),
  848.       N2 is N1-N+1,
  849.       arg(N2,T,A),
  850.       N3 is N-1,
  851.       !,
  852.       rewrite_args_2(N3,T,_,_,_,_,Lim,1,As,F).
  853.     rewrite_args_2(N,T,Sp,Ds,Vs,Cs,Lim,_,As,F) :-
  854.       !,
  855.       rewrite_args_1(N,T,Sp,Ds,Vs,Cs,Lim,As,F).
  856.  
  857. %%%
  858. %%% Rewrite_args_equational fully reduces the arguments of a term using
  859. %%% equational rewrite rules.
  860. %%%
  861.     rewrite_args_equational(T,_,_,_,_,_,T,0) :-
  862.       not(equational_rewrite),
  863.       !.
  864.     rewrite_args_equational(T1,Sp,Ds,Vs,Cs,Lim,T2,F1) :-
  865.       functor(T1,F,N),
  866.       rewrite_args_equational_1(N,T1,Sp,Ds,Vs,Cs,Lim,As,F1),
  867.       T2=..[F|As],
  868.       !.
  869.     rewrite_args_equational_1(0,_,_,_,_,_,_,[],0) :- !.
  870.     rewrite_args_equational_1(N,T,Sp,Ds,Vs,Cs,Lim,[A|As],F1) :-
  871.       functor(T,_,N1),
  872.       N2 is N1-N+1,
  873.       arg(N2,T,A1),
  874.       rewrite_term_5(A1,Sp,Ds,Vs,Cs,Lim,A,0,F),
  875.       N3 is N-1,
  876.       rewrite_args_equational_2(N3,T,Sp,Ds,Vs,Cs,Lim,F,As,F1),
  877.       !.
  878.     rewrite_args_equational_2(0,_,_,_,_,_,_,1,[],1) :- !.
  879.     rewrite_args_equational_2(N,T,_,_,_,_,Lim,1,[A|As],F) :-
  880.       functor(T,_,N1),
  881.       N2 is N1-N+1,
  882.       arg(N2,T,A),
  883.       N3 is N-1,
  884.       !,
  885.       rewrite_args_equational_2(N3,T,_,_,_,_,Lim,1,As,F).
  886.     rewrite_args_equational_2(N,T,Sp,Ds,Vs,Cs,Lim,_,As,F) :-
  887.       !,
  888.       rewrite_args_equational_1(N,T,Sp,Ds,Vs,Cs,Lim,As,F).
  889.  
  890. %%%
  891. %%% Print_rewrite prints a rewritten term.
  892. %%%
  893.     print_rewrite(_,_) :- 
  894.       not(outrwt),
  895.       !.
  896.     print_rewrite(C1,C2) :-
  897.       C1==C2,
  898.       !.
  899.     print_rewrite(C1,C2) :-
  900.       write_line(10,'rewriting:'),
  901.       not(not(print_rewrite_1(C1,C2))),
  902.       !.
  903.     print_rewrite_1(C1,C2) :-
  904.       vars_tail(C1,V),
  905.       var_list(V),
  906.       write_line(13,'orig: ',C1),
  907.       write_line(13,'new:  ',C2),
  908.       !.
  909.  
  910. %%%
  911. %%% Rewrite_rewrite_rules rewrites all non-user rewrite rules.
  912. %%%
  913.     rewrite_rewrite_rules(0).
  914.     rewrite_rewrite_rules(1) :-
  915.       bagof1(rwr(R,V,F,0),rwr(R,V,F,0),Rs),
  916.       rewrite_rewrite_rules_1(Rs,Fs),
  917.       ((prove_result(_);time_overflow) -> true; (
  918.         or_list(Fs,F),
  919.         rewrite_rewrite_rules(F)
  920.       )).
  921.     rewrite_rewrite_rules_1([],[]) :- !.
  922.     rewrite_rewrite_rules_1([rwr(R,V,F,U)|Rs],[0|Fs]) :-
  923.       rewrite_rule_ref(rwr(R,V,F,U),Ref),
  924.       instance_rewrite_rule_ref(Ref),
  925.       !,
  926.       erase_rewrite_rule(rwr(R,V,F,U)),
  927.       rewrite_rewrite_rules_1(Rs,Fs).
  928.     rewrite_rewrite_rules_1([rwr(R,var(V1,V1,V),F1,U)|Rs],[F2|Fs]) :-
  929.       constrained_rewriting,
  930.       !,
  931.       arg(1,R,S),
  932.       arg(2,R,T),
  933.       ((restricted_rewrite ->
  934.         abolish(restricted_rewrite,0),
  935.         rewrite_clause_with_constraint([S=T],_,_,[],Cts1),
  936.         assert(restricted_rewrite)
  937.       ); (
  938.         rewrite_clause_with_constraint([S=T],_,_,[],Cts1)
  939.       )),
  940.       rewrite_rewrite_rules_2(Cts1,S=T,Cts2),
  941.       (Cts1\==Cts2 -> (
  942.         Cts3=Cts2
  943.       ); (
  944.         erase_rewrite_rule(rwr(R,var(V1,V1,V),F1,U)),
  945.         (restricted_rewrite -> (
  946.           abolish(restricted_rewrite,0),
  947.           rewrite_clause_with_constraint([S=T],_,_,[],Cts3),
  948.           assert(restricted_rewrite)
  949.         ); (
  950.           rewrite_clause_with_constraint([S=T],_,_,[],Cts3)
  951.         ))
  952.       )),
  953.       rewrite_rewrite_rules_3(Cts3,Fs1),
  954.       or_list(Fs1,F2),
  955.       ((prove_result(_);time_overflow) -> (
  956.         Fs=[]
  957.       ); (
  958.         rewrite_rewrite_rules_1(Rs,Fs)
  959.       )).
  960.     rewrite_rewrite_rules_1([rwr(R,var(V1,V1,V),F1,U)|Rs],[F2|Fs]) :-
  961.       arg(1,R,S),
  962.       arg(2,R,T),
  963.       (restricted_rewrite -> (
  964.         abolish(restricted_rewrite,0),
  965.         remove_rewrite_rule(S=T),
  966.         rewrite_term(S,_,_,0,S1,_),
  967.         restore_rewrite_rule(S=T),
  968.         remove_rewrite_rule(T=S),
  969.         rewrite_term(T,_,_,0,T1,_),
  970.         restore_rewrite_rule(T=S),
  971.         assert(restricted_rewrite)
  972.       ); (
  973.         remove_rewrite_rule(S=T),
  974.         rewrite_term(S,_,_,0,S1,_),
  975.         restore_rewrite_rule(S=T),
  976.         remove_rewrite_rule(T=S),
  977.         rewrite_term(T,_,_,0,T1,_),
  978.         restore_rewrite_rule(T=S)
  979.       )),
  980.       ((S\==S1; T\==T1) -> ( 
  981.         erase_rewrite_rule(rwr(R,var(V1,V1,V),F1,U)),
  982.         generate_rewrite_rule([S1=T1],F2),
  983.         ((F2==1, check_unit_conflict_C(S1=T1)) -> (
  984.           Fs=[],
  985.           assert_tryresult('unsatisfiable'),
  986.           assert_prooftype('UC')
  987.           ); (
  988.             rewrite_rewrite_rules_1(Rs,Fs)
  989.         ))
  990.       ); (
  991.         F2=0,
  992.         rewrite_rewrite_rules_1(Rs,Fs)
  993.       )).
  994.     rewrite_rewrite_rules_2([],_,[]) :- !.
  995.     rewrite_rewrite_rules_2([[[S1=T1],_,_]|Cts1],S=T,Cts2) :-
  996.       (S1=T1)==(S=T),
  997.       !,
  998.       rewrite_rewrite_rules_2(Cts1,S=T,Cts2).
  999.     rewrite_rewrite_rules_2([Ct|Cts1],S=T,[Ct|Cts2]) :-
  1000.       rewrite_rewrite_rules_2(Cts1,S=T,Cts2).
  1001.     rewrite_rewrite_rules_3([],[]) :- !.
  1002.     rewrite_rewrite_rules_3([[[S=T],_,_]|Cts],[F|Fs]) :-
  1003.       generate_rewrite_rule([S=T],F),
  1004.       (F==1 -> (
  1005.         (check_unit_conflict_C(S=T) -> (
  1006.           Fs=[],
  1007.           assert_tryresult('unsatisfiable'),
  1008.           assert_prooftype('UC')
  1009.         ); (
  1010.           rewrite_rewrite_rules_4(Cts,[],Cts1),
  1011.           rewrite_rewrite_rules_3(Cts1,Fs)
  1012.         ))
  1013.       ); (
  1014.         rewrite_rewrite_rules_3(Cts,Fs)
  1015.       )).
  1016.     rewrite_rewrite_rules_4([],Cts,Cts) :- !.
  1017.     rewrite_rewrite_rules_4([[[S=T],0,Con]|Cts1],Cts2,Cts3) :-
  1018.       !,
  1019.       (restricted_rewrite -> (
  1020.         abolish(restricted_rewrite,0),
  1021.         rewrite_clause_with_constraint([S=T],_,_,Con,Cts4),
  1022.         assert(restricted_rewrite)
  1023.       ); (
  1024.         rewrite_clause_with_constraint([S=T],_,_,Con,Cts4)
  1025.       )),        
  1026.       append(Cts2,Cts4,Cts5),
  1027.       rewrite_rewrite_rules_4(Cts1,Cts5,Cts3).
  1028.     rewrite_rewrite_rules_4([Ct|Cts1],Cts2,Cts3) :-
  1029.       append(Cts2,[Ct],Cts4),
  1030.       rewrite_rewrite_rules_4(Cts1,Cts4,Cts3).
  1031.  
  1032. %%%
  1033. %%% Erase_rewrite_rule erases a rewrite rule.
  1034. %%%
  1035.     erase_rewrite_rule(rwr(R,V,F,U)) :-
  1036.       abolish_rewrite_caches,
  1037.       not(not(erase_rewrite_rule_1(rwr(R,V,F,U)))),
  1038.       not(not(eras  rewrite_rule_2(R,V))),
  1039.       !.
  1040.     erase_rewrite_rule(_).
  1041.     erase_rewrite_rule_1(rwr(R,var(V1,V1,V),F,U)) :-
  1042.       const_list(V),
  1043.       retract(rwr(R,var(V1,V1,V),F,U)),
  1044.       !.
  1045.     erase_rewrite_rule_2(R,var(V1,V1,V)) :-
  1046.       outrwr,
  1047.       !,
  1048.       arg(1,R,S),
  1049.       arg(2,R,T),
  1050.       var_list(V),
  1051.       write_line(10,'deleting rewrite rule: ',S,'->',T),
  1052.       !.
  1053.     erase_rewrite_rule_2(_,_).
  1054.  
  1055. %%%
  1056. %%% Generate_rewrite_rule generates a rewrite rule from an equation.
  1057. %%%
  1058.     generate_rewrite_rule(_,0) :-
  1059.       not(auto_orient),
  1060.       !.
  1061.     generate_rewrite_rule([S=T],0) :-
  1062.       S==T,
  1063.       !.
  1064.     generate_rewrite_rule([S=T],F) :-
  1065.       !,
  1066.       order_term(S,O,T),
  1067.       generate_rewrite_rule_1(S,O,T,F).
  1068.     generate_rewrite_rule(_,0).
  1069.     generate_rewrite_rule_1(S,>,T,F) :-
  1070.       !,
  1071.       assert_rewrite_rule(0,(S->T),F).
  1072.     generate_rewrite_rule_1(S,<,T,F) :-
  1073.       !,
  1074.       assert_rewrite_rule(0,(T->S),F).
  1075.     generate_rewrite_rule_1(S,_,T,F) :-
  1076.       equational_rewrite,
  1077.       !,
  1078.       assert_rewrite_rule(0,(S=T),F).
  1079.     generate_rewrite_rule_1(_,_,_,0).
  1080.  
  1081. %%%
  1082. %%% Assert_rewrite_rule asserts a rewrite rule into the database.
  1083. %%%
  1084.     assert_rewrite_rule(_,R,0) :-
  1085.       arg(1,R,S),
  1086.       priority_bound(PrioBound),
  1087.       calculate_priority_clause([S],sp(0,0,0),ds(0,0,0),pr(_,_,_,_,Pr)),
  1088.       Pr > PrioBound,
  1089.       !,
  1090.       assert_once(over_priohm).
  1091.     assert_rewrite_rule(_,R,0) :-
  1092.       arg(2,R,T),
  1093.       priority_bound(PrioBound),
  1094.       calculate_priority_clause([T],sp(0,0,0),ds(0,0,0),pr(_,_,_,_,Pr)),
  1095.       Pr > PrioBound,
  1096.       !,
  1097.       assert_once(over_priohm).
  1098.     assert_rewrite_rule(U,(S->T),1) :-
  1099.       vars_tail((S->T),V),
  1100.       not(instance_rewrite_rule((S->T),V)),
  1101.       !,
  1102.       assert_rewrite_rule_1((S->T),F),
  1103.       abolish_rewrite_caches,
  1104.       linearize_term((S->T),(S1->T1),V1,V2),
  1105.       assertz(rwr((S1->T1),var(V1,V2,V),F,U)),
  1106.       print_rewrite_rule(rwr((S1->T1),var(V1,V2,V),F,U)),
  1107.       !.
  1108.     assert_rewrite_rule(U,(S->T),0) :- !.
  1109.     assert_rewrite_rule(U,S=T,F) :-
  1110.       assert_rewrite_rule_3(S=T,U,F1),
  1111.       assert_rewrite_rule_3(T=S,U,F2),
  1112.       assert_rewrite_rule_6(F1,F2,F).
  1113.     assert_rewrite_rule_1((S->T),0) :-
  1114.       list_vars(T,Vs),
  1115.       assert_rewrite_rule_2(S,T,Vs),
  1116.       !.
  1117.     assert_rewrite_rule_1(S=T,0) :-
  1118.       list_vars(T,Vs),
  1119.       assert_rewrite_rule_2(S,T,Vs),
  1120.       !.
  1121.     assert_rewrite_rule_1(_,1).
  1122.     assert_rewrite_rule_2(S,T,[]).
  1123.     assert_rewrite_rule_2(S,T,[V|Vs]) :-
  1124.       count_occurrences(V,S,Ns),
  1125.       count_occurrences(V,T,Nt),
  1126.       Ns>=Nt,
  1127.       !,
  1128.       assert_rewrite_rule_2(S,T,Vs).
  1129.     assert_rewrite_rule_3(S=T,U,1) :-
  1130.       vars_tail(S=T,V),
  1131.       not(instance_rewrite_rule(S=T,V)),
  1132.       list_vars(T,Vs),
  1133.       assert_rewrite_rule_4(S=T,Vs,F),
  1134.       !,
  1135.       abolish_rewrite_caches,
  1136.       linearize_term(S=T,S1=T1,V1,V2),
  1137.       assertz(rwr(S1=T1,var(V1,V2,V),F,U)),
  1138.       print_rewrite_rule(rwr(S1=T1,var(V1,V2,V),F,U)).
  1139.     assert_rewrite_rule_3(_,_,0).
  1140.     assert_rewrite_rule_4(_,[],0).
  1141.     assert_rewrite_rule_4(S=T,[V|Vs],F) :-
  1142.       count_occurrences(V,S,Ns),
  1143.       Ns\==0,
  1144.       !,
  1145.       assert_rewrite_rule_5(S=T,[V|Vs],Ns,F).
  1146.     assert_rewrite_rule_5(S=T,[V|Vs],Ns,F) :-
  1147.       count_occurrences(V,T,Nt),
  1148.       Ns>=Nt,
  1149.       !,
  1150.       assert_rewrite_rule_4(S=T,Vs,F).
  1151.     assert_rewrite_rule_5(S=T,[_|Vs],_,1) :-
  1152.       !,
  1153.       assert_rewrite_rule_4(S=T,Vs,_).
  1154.     assert_rewrite_rule_6(1,_,1) :- !.
  1155.     assert_rewrite_rule_6(_,1,1) :- !.
  1156.     assert_rewrite_rule_6(_,_,0).
  1157.  
  1158. %%%
  1159. %%% Instance_rewrite_rule_ref determines if an existing rewrite rule is an
  1160. %%% instance of another existing rewrite rule.  We first check for duplicate
  1161. %%% to improve performance (at least under Quintus Prolog).
  1162. %%% 
  1163.     instance_rewrite_rule_ref(Ref) :-
  1164.       const_list(V),
  1165.       clause(rwr(R,var(V1,V1,V),_,_),true,Ref),
  1166.       instance_rewrite_rule_ref_1(R,Ref),
  1167.       !.
  1168.     instance_rewrite_rule_ref_1((S->T),Ref) :-
  1169.       const_list(V),
  1170.       clause(rwr((S->T),var(V1,V1,V),_,_),true,Ref1),
  1171.       Ref1\==Ref,
  1172.       !.
  1173.     instance_rewrite_rule_ref_1((S->T),Ref) :-
  1174.       const_list(V),
  1175.       clause(rwr((T->S),var(V1,V1,V),_,_),true,Ref1),
  1176.       Ref1\==Ref,
  1177.       !.
  1178.     instance_rewrite_rule_ref_1(S=T,Ref) :-
  1179.       const_list(V),
  1180.       clause(rwr(S=T,var(V1,V1,V),_,_),true,Ref1),
  1181.       Ref1\==Ref,
  1182.       !.
  1183.     instance_rewrite_rule_ref_1((S->T),Ref) :-
  1184.       clause(rwr((S->T),var(V1,V1,_),_,_),true,Ref1),
  1185.       Ref1\==Ref,
  1186.       !.
  1187.     instance_rewrite_rule_ref_1((S->T),Ref) :-
  1188.       clause(rwr((T->S),var(V1,V1,_),_,_),true,Ref1),
  1189.       Ref1\==Ref,
  1190.       !.
  1191.     instance_rewrite_rule_ref_1(S=T,Ref) :-
  1192.       clause(rwr(S=T,var(V1,V1,_),_,_),true,Ref1),
  1193.       Ref1\==Ref,
  1194.       !.
  1195.  
  1196. %%%
  1197. %%% Instance_rewrite_rule determines if a rewrite rule is an instance of an
  1198. %%% existing rewrite rule.  We first check for duplicate to improve performance
  1199. %%% (at least under Quintus Prolog).
  1200. %%%
  1201.     instance_rewrite_rule(R,V) :-
  1202.       not(not(instance_rewrite_rule_1(R,V))),
  1203.       !.
  1204.     instance_rewrite_rule_1(R,V) :-
  1205.       const_list(V),
  1206.       instance_rewrite_rule_2(R),
  1207.       !.
  1208.     instance_rewrite_rule_2((S->T)) :-
  1209.       const_list(V),
  1210.       rwr((S->T),var(V1,V1,V),_,_),
  1211.       !.
  1212.     instance_rewrite_rule_2((S->T)) :-
  1213.       const_list(V),
  1214.       rwr((T->S),var(V1,V1,_),_,_),
  1215.       !.
  1216.     instance_rewrite_rule_2(S=T) :-
  1217.       const_list(V),
  1218.       rwr(S=T,var(V1,V1,_),_,_),
  1219.       !.
  1220.     instance_rewrite_rule_2((S->T)) :-
  1221.       rwr((S->T),var(V1,V1,_),_,_),
  1222.       !.
  1223.     instance_rewrite_rule_2((S->T)) :-
  1224.       rwr((T->S),var(V1,V1,_),_,_),
  1225.       !.
  1226.     instance_rewrite_rule_2(S=T) :-
  1227.       rwr(S=T,var(V1,V1,_),_,_),
  1228.       !.
  1229.  
  1230. %%%
  1231. %%% Lex_gt determines if a function or predicate is greater than another
  1232. %%% function or predicate.  The following ordering is used:
  1233. %%%   - user specified ordering
  1234. %%%   - arity (constants have arity 0)
  1235. %%%   - standard Prolog ordering of the symbol representing the function or
  1236. %%%     predicate
  1237. %%%
  1238.     lex_gt(S,Sa,T,Ta) :-
  1239.       rwo([S,Sa,T,Ta]),
  1240.       !.
  1241.     lex_gt(S,Sa,T,Ta) :-
  1242.       rwo([T,Ta,S,Sa]),
  1243.       !,
  1244.       fail.
  1245.     lex_gt(S,Sa,T,Ta) :-
  1246.       rwe([S,Sa,T,Ta]),
  1247.       !,
  1248.       fail.
  1249.     lex_gt(S,Sa,T,Ta) :-
  1250.       Sa>Ta,
  1251.       !.
  1252.     lex_gt(S,A,T,A) :-
  1253.       S@>T,
  1254.       !.
  1255.  
  1256. %%%
  1257. %%% Lex_eq determines if a function or predicate is equal to another function
  1258. %%% or predicate.  The following ordering is used:
  1259. %%%   - user specified ordering
  1260. %%%   - arity (constants have arity 0)
  1261. %%%   - standard Prolog ordering of the symbol representing the function or
  1262. %%%     predicate
  1263. %%%
  1264.     lex_eq(S,Sa,T,Ta) :-
  1265.       rwo([S,Sa,T,Ta]),
  1266.       !,
  1267.       fail.
  1268.     lex_eq(S,Sa,T,Ta) :-
  1269.       rwo([T,Ta,S,Sa]),
  1270.       !,
  1271.       fail.
  1272.     lex_eq(S,Sa,T,Ta) :-
  1273.       rwe([S,Sa,T,Ta]),
  1274.       !.
  1275.     lex_eq(F,A,F,A).
  1276.  
  1277. %%%
  1278. %%% Order_term determines the order (<,=,>,!) of two terms.
  1279. %%%
  1280.     order_term(T1,O,T2) :-
  1281.       term_ordering(lpo),
  1282.       !,
  1283.       order_term_lpo(T1,O,T2),
  1284.       !.
  1285.     order_term(_,_,_) :-
  1286.       term_ordering(X),
  1287.       !,
  1288.       write_line(5,'Invalid term ordering: ',X),
  1289.       abort.
  1290.     order_term(_,_,_) :-
  1291.       write_line(5,'No term ordering'),
  1292.       abort.
  1293.  
  1294. %%%
  1295. %%% Order_term_lpo determines the order (<,=,>,!) of two terms with respect to
  1296. %%% lexicographic path ordering.
  1297. %%%
  1298.     order_term_lpo(S,O,T) :-
  1299.       term_size_structure(S,SSize),
  1300.       term_size_structure(T,TSize),
  1301.       order_term_lpo_1(S,O,T,SSize,TSize).     
  1302.     order_term_lpo_1(S,O,T,_,_) :-
  1303.       S==T,
  1304.       !,
  1305.       O='='.
  1306.     order_term_lpo_1(S,O,T,_,_) :-
  1307.       var(S),
  1308.       !,
  1309.       (occurs_check(T,S) -> (
  1310.         O='!'
  1311.       ); (
  1312.         O='<'
  1313.       )).
  1314.     order_term_lpo_1(S,O,T,_,_) :-
  1315.       var(T),
  1316.       !,
  1317.       (occurs_check(S,T) -> (
  1318.         O='!'
  1319.       ); (
  1320.         O='>'
  1321.       )).
  1322.     order_term_lpo_1(S,O,T,SSize,TSize) :-
  1323.       vars_tail({S,T},V),
  1324.       arg(1,SSize,Size1),
  1325.       arg(1,TSize,Size2),
  1326.       Size3 is (Size1*10000)+Size2,
  1327.       ((not(not((
  1328.           const_list(V),
  1329.           cached_term_order(Size3,S,T,V,O1),
  1330.           assert(cto_order(O1)))))) -> (
  1331.         retract(cto_order(O1))
  1332.       ); (
  1333.         Size4 is (Size2*10000)+Size1,
  1334.         vars_tail({T,S},V1),
  1335.         ((not(not((
  1336.             const_list(V1),
  1337.             cached_term_order(Size4,T,S,V1,O2),
  1338.             assert(cto_order(O2)))))) -> (
  1339.           retract(cto_order(O2)),
  1340.           order_term_lpo_9(O2,O1)
  1341.         ); (
  1342.           order_term_lpo_2(S,O1,T,SSize,TSize),
  1343.           counter(cached_term_order_count,N),
  1344.           cache_size(CacheSize),
  1345.           (N > CacheSize -> (
  1346.             write_line(0,'*** cleared cached_term_order: ',N),
  1347.             set_counter(cached_term_order_count,0),
  1348.             abolish(cached_term_order,5)
  1349.           ); true),
  1350.           Size5 is Size1+Size2,
  1351.           increment_counter(cached_term_order_count,Size5),
  1352.           assert(cached_term_order(Size3,S,T,V,O1))
  1353.         ))
  1354.       )),
  1355.       O=O1.
  1356.     order_term_lpo_2(S,O,T,SSize,TSize) :-
  1357.       functor(S,_,N),
  1358.       order_term_lpo_10(N,S,T,So,F,SSize,TSize),
  1359.       !,
  1360.       order_term_lpo_3(F,S,O,T,So,SSize,TSize).
  1361.     order_term_lpo_3(1,_,>,_,_,_,_) :- !.
  1362.     order_term_lpo_3(0,S,O,T,So,SSize,TSize) :-
  1363.       functor(T,_,N),
  1364.       order_term_lpo_10(N,T,S,To,F,TSize,SSize),
  1365.       !,
  1366.       order_term_lpo_4(F,S,O,T,So,To,SSize,TSize).
  1367.     order_term_lpo_4(1,_,<,_,_,_,_,_) :- !.
  1368.     order_term_lpo_4(0,S,O,T,So,To,SSize,TSize) :-
  1369.       functor(S,Sf,Sn),
  1370.       functor(T,Tf,Tn),
  1371.       lex_eq(Sf,Sn,Tf,Tn),
  1372.       !,
  1373.       order_term_lpo_5(Sn,S,O,T,So,To,SSize,TSize).
  1374.     order_term_lpo_4(0,S,O,T,So,To,_,_) :-
  1375.       functor(S,Sf,Sn),
  1376.       functor(T,Tf,Tn),
  1377.       lex_gt(Sf,Sn,Tf,Tn),
  1378.       !,
  1379.       order_term_lpo_8(To,O).
  1380.     order_term_lpo_4(0,S,O,T,So,To,_,_) :-
  1381.       functor(S,Sf,Sn),
  1382.       functor(T,Tf,Tn),
  1383.       lex_gt(Tf,Tn,Sf,Sn),
  1384.       !,
  1385.       order_term_lpo_8(So,O1),
  1386.       order_term_lpo_9(O1,O).
  1387.     order_term_lpo_4(0,_,!,_,_,_,_,_).
  1388.     order_term_lpo_5(0,_,O,_,_,_,_,_) :-
  1389.       !,
  1390.       O='='.
  1391.     order_term_lpo_5(N,S,O,T,[_|So],[_|To],SSize,TSize) :-
  1392.       functor(S,_,N1),
  1393.       N2 is N1-N+1,
  1394.       arg(N2,S,Si),
  1395.       arg(N2,T,Ti),
  1396.       N3 is N2+1,
  1397.       arg(N3,SSize,SiSize),
  1398.       arg(N3,TSize,TiSize),
  1399.       order_term_lpo_1(Si,O1,Ti,SiSize,TiSize),
  1400.       order_term_lpo_6(O1,N,S,T,So,To,O,SSize,TSize).
  1401.     order_term_lpo_6(=,N,S,T,So,To,O,SSize,TSize) :-
  1402.       N1 is N-1,
  1403.       order_term_lpo_5(N1,S,O,T,So,To,SSize,TSize).
  1404.     order_term_lpo_6(!,_,_,_,_,_,!,_,_).
  1405.     order_term_lpo_6(>,_,_,_,_,To,O,_,_) :-
  1406.       (order_term_lpo_7(To) -> (
  1407.         O='>'
  1408.       ); (
  1409.         O='!'
  1410.       )).
  1411.     order_term_lpo_6(<,_,_,_,So,_,O,_,_) :-
  1412.       (order_term_lpo_7(So) -> (
  1413.         O='<'
  1414.       ); (
  1415.         O='!'
  1416.       )).
  1417.     order_term_lpo_7([]) :- !.
  1418.     order_term_lpo_7([<|So]) :-
  1419.       order_term_lpo_7(So).
  1420.     order_term_lpo_8([],O) :-
  1421.       !,
  1422.       O='>'.
  1423.     order_term_lpo_8([!|_],O) :-
  1424.       !,
  1425.       O='!'.
  1426.     order_term_lpo_8([<|So],O) :-
  1427.       order_term_lpo_8(So,O).
  1428.     order_term_lpo_9(>,O) :-
  1429.       !,
  1430.       O='<'.
  1431.     order_term_lpo_9(<,O) :-
  1432.       !,
  1433.       O='>'.
  1434.     order_term_lpo_9(O,O).
  1435.     order_term_lpo_10(0,_,_,[],0,_,_) :- !.
  1436.     order_term_lpo_10(N,S,T,[O|So],F,SSize,TSize) :-
  1437.       functor(S,_,N1),
  1438.       N2 is N1-N+1,
  1439.       arg(N2,S,Si),
  1440.       N3 is N2+1,
  1441.       arg(N3,SSize,SiSize),
  1442.       order_term_lpo_1(Si,O,T,SiSize,TSize),
  1443.       order_term_lpo_11(O,N,S,T,So,F,SSize,TSize).
  1444.     order_term_lpo_11(>,_,_,_,_,1,_,_) :- !.
  1445.     order_term_lpo_11(=,_,_,_,_,1,_,_) :- !.
  1446.     order_term_lpo_11(_,N,S,T,So,F,SSize,TSize) :-
  1447.       N1 is N-1,
  1448.       order_term_lpo_10(N1,S,T,So,F,SSize,TSize).
  1449.  
  1450. %%%
  1451. %%% Assert_saved_term_order asserts intermediate term ordering data into the
  1452. %%% database.
  1453. %%%
  1454.     assert_saved_term_order(S,T,V,F) :-
  1455.       memoize_term_order,
  1456.       !,
  1457.       assert(saved_term_order(S,T,V,F)),
  1458.       !.
  1459.     assert_saved_term_order(_,_,_,_) :- !.
  1460.  
  1461. %%%
  1462. %%% Check_saved_term_order checks to see if the ordering as previously been
  1463. %%% computed.
  1464.       check_saved_term_order(T1,T2,V,F) :-
  1465.         memoize_term_order,
  1466.         not(not(check_saved_term_order_1(T1,T2,V))),
  1467.         retract(csto_temp(F)),
  1468.         !.
  1469.       check_saved_term_order_1(T1,T2,V) :-
  1470.         const_list(V),
  1471.         saved_term_order(T1,T2,_,1),
  1472.         asserta(csto_temp(1)),
  1473.         !.
  1474.       check_saved_term_order_1(T1,T2,V) :-
  1475.         const_list(V),
  1476.         saved_term_order(T1,T2,V,0),
  1477.         asserta(csto_temp(0)),
  1478.         !.
  1479.  
  1480. %%%
  1481. %%% Print_rewrite_rule prints a rewrite rule.
  1482. %%%
  1483.     print_rewrite_rule(_) :-
  1484.       not(outrwr),
  1485.       !.
  1486.     print_rewrite_rule(R) :-
  1487.       not(not(print_rewrite_rule1(R))),
  1488.       !.
  1489.     print_rewrite_rule1(rwr((S->T),var(V1,V1,V),F,U)) :-
  1490.       var_list(V),
  1491.       write_line(10,'rewrite rule: ',S,'->',T),
  1492.       !.
  1493.     print_rewrite_rule1(rwr(S=T,var(V1,V1,V),F,U)) :-
  1494.       var_list(V),
  1495.       write_line(10,'rewrite rule: ',S,'=',T),
  1496.       !.
  1497.  
  1498. %%%
  1499. %%% Orient_equation orients an equation.
  1500. %%%
  1501.     orient_equation(S=T,T=S) :-
  1502.       order_term(T,>,S),
  1503.       !.
  1504.     orient_equation(L,L).
  1505.  
  1506. %%%
  1507. %%% Assert_rewrite_rule_equations asserts the corresponding equation for each
  1508. %%% rewrite rule if the equation is not already asserted.
  1509. %%%
  1510.     assert_rewrite_rule_equations :-
  1511.       rwr((S->T),var(V1,V1,V),_,_),
  1512.       clause_size([S=T],CSize),
  1513.       assert_rewrite_rule_equations_1(CSize,[S=T],C,V),
  1514.       fail.
  1515.     assert_rewrite_rule_equations :-
  1516.       rwr(S=T,var(V1,V1,V),_,_),
  1517.       clause_size([S=T],CSize),
  1518.       assert_rewrite_rule_equations_1(CSize,[S=T],C,V),
  1519.       fail.
  1520.     assert_rewrite_rule_equations.
  1521.     assert_rewrite_rule_equations_1(CSize,[S=T],C,V) :-
  1522.       not(duplicate_clause_C(CSize,[S=T],V)),
  1523.       vars_literals([S=T],W),
  1524.       compute_V_lits(W,0,NLits),
  1525.       linearize_term([S=T],C,V1,V2),
  1526.       set_user_support(C,S1),
  1527.       set_sr_status(S1,C,Sp,Ds),
  1528.       calculate_priority_clause(C,Sp,Ds,Pr),
  1529.       assert_rewrite_rule_equations_2(Pr),
  1530.       list_of_Ns(C,0,Dis),
  1531.       assertz(sent_C(cl(CSize,NLits,by(C,V1,V2,V,W),0,Sp,Ds,0,Dis,Pr))).
  1532.     assert_rewrite_rule_equations_2(Pr) :-
  1533.       priority_bound(PrioBound),
  1534.       !,
  1535.       check_prioritybound(Pr,PrioBound).
  1536.     assert_rewrite_rule_equations_2(_).
  1537.  
  1538. %%%
  1539. %%% Set_user_support set the user support flag as follows:
  1540. %%%   if current support includes user support then
  1541. %%%     if the user support is "o" then
  1542. %%%       if the clause is a unit clause then
  1543. %%%         set user support flag to 1
  1544. %%%       else set user support flag to 0
  1545. %%%     else set user support flag to 0
  1546. %%%   else set user support flag to 1
  1547. %%%     
  1548.     set_user_support(C,S1) :-
  1549.       current_support(sup(1,_,_,_)),
  1550.       !,
  1551.       set_user_support_1(C,S1).
  1552.     set_user_support(_,1).
  1553.     set_user_support_1(C,S1) :-
  1554.       user_support(o),
  1555.       !,
  1556.       set_user_support_2(C,S1).
  1557.     set_user_support_1(_,0).
  1558.     set_user_support_2([L],1) :- !.
  1559.     set_user_support_2(_,0).
  1560.  
  1561. %%%
  1562. %%% remove_rewrite_rule(E)
  1563. %%%   input
  1564. %%%     E  : equation
  1565. %%%
  1566.     remove_rewrite_rule(E) :-
  1567.       vars_tail(E,V),
  1568.       assert(dont_use_rewrite_rule_outer(E,V)).
  1569.  
  1570. %%%
  1571. %%% restore_rewrite_rule(E)
  1572. %%%   input
  1573. %%%     E : equation
  1574. %%%
  1575.     restore_rewrite_rule(E) :-
  1576.       vars_tail(E,V),
  1577.       retract(dont_use_rewrite_rule_outer(E,V)).
  1578.  
  1579. %%%
  1580. %%% print_rewrite_with_constraint(T,Cts) - prints a term and its constrained
  1581. %%% normal forms
  1582. %%%   input
  1583. %%%     T   : term
  1584. %%%     Cts : set of term/flag/constraint triples representing the constrained
  1585. %%%           normal forms of T
  1586. %%%
  1587.     print_rewrite_with_constraint(_,_) :- 
  1588.       not(outrwt),
  1589.       !.
  1590.     print_rewrite_with_constraint(T,Cts) :-
  1591.       not(not(print_rewrite_with_constraint_1(T,Cts))).
  1592.     print_rewrite_with_constraint_1(T,Cts) :-
  1593.       vars_tail(T,V),
  1594.       var_list(V),
  1595.       write_line(10,'rewriting:'),
  1596.       write_line(13,'orig: ',T),
  1597.       print_rewrite_with_constraint_2(Cts).
  1598.     print_rewrite_with_constraint_2([]) :- !.
  1599.     print_rewrite_with_constraint_2([[T,_,Con]|Cts1]) :-
  1600.       write_line(13,'new:  ',T,' ',Con),
  1601.       print_rewrite_with_constraint_3(Cts1).
  1602.     print_rewrite_with_constraint_3([]) :- !.
  1603.     print_rewrite_with_constraint_3([[T,_,Con]|Cts1]) :-
  1604.       write_line(13,'      ',T,' ',Con),
  1605.       print_rewrite_with_constraint_3(Cts1).
  1606.  
  1607. %%%
  1608. %%% rewrite_clause_with_constraint(C,Sp,Ds,Con,Cts)
  1609. %%%   input
  1610. %%%     C   : clause
  1611. %%%     Sp  : support data for computing priority
  1612. %%%     Ds  : distance data for computing priority
  1613. %%%     Con : constraint
  1614. %%%   output
  1615. %%%     Cts : set of simplified clause/flag/constraint triples obtained by
  1616. %%%           rewriting C under the constraint Con.  Flag is set as follows:
  1617. %%%             0 -- clause fully rewritten
  1618. %%%             1 -- rewrite terminated early due to priority
  1619. %%%   notes
  1620. %%%     If Sp is uninstantiated, the priority is not checked after using a
  1621. %%%       marked equational rule.
  1622. %%%
  1623.     rewrite_clause_with_constraint(C,_,_,Con,[[C,0,Con]]) :-
  1624.       not(rwr(_,_,_,_)),
  1625.       !.
  1626.     rewrite_clause_with_constraint(C,Sp,Ds,Con,Cts) :-
  1627.       (precompute_constraints -> (
  1628.         rewrite_clause_with_constraint_1(C,[],Vss1),
  1629.         identical_delete_all_duplicates(Vss1,Vss2),
  1630.         rewrite_clause_with_constraint_5(Vss2,Conss),
  1631.         bagof1(Con1,rewrite_clause_with_constraint_8(Conss,[],Con1),Cons),
  1632.         rewrite_clause_with_constraint_9(Cons,C,Sp,Ds,Con,[],Cts)
  1633.       ); (
  1634.         rewrite_clause_with_constraint_9([[]],C,Sp,Ds,Con,[],Cts)
  1635.       )).
  1636.     rewrite_clause_with_constraint_1([],Vss,Vss) :- !.
  1637.     rewrite_clause_with_constraint_1([not S=T|C],Vss1,Vss2) :-
  1638.       restricted_rewrite,
  1639.       !,
  1640.       (nonvar(S) -> (
  1641.         functor(S,_,N),
  1642.         rewrite_clause_with_constraint_4(N,S,[],Vss3)
  1643.       ); (
  1644.         Vss3=[]
  1645.       )),
  1646.       (nonvar(T) -> (
  1647.         rewrite_clause_with_constraint_2(T,Vss4)
  1648.       ); (
  1649.         Vss4=[]
  1650.       )),
  1651.       append(Vss3,Vss4,Vss5),
  1652.       append(Vss1,Vss5,Vss6),
  1653.       rewrite_clause_with_constraint_1(C,Vss6,Vss2).
  1654.     rewrite_clause_with_constraint_1([not L|C],Vss1,Vss2) :-
  1655.       !,
  1656.       functor(L,_,N),
  1657.       rewrite_clause_with_constraint_4(N,L,[],Vss3),
  1658.       append(Vss1,Vss3,Vss4),
  1659.       rewrite_clause_with_constraint_1(C,Vss4,Vss2).
  1660.     rewrite_clause_with_constraint_1([L|C],Vss1,Vss2) :-
  1661.       functor(L,_,N),
  1662.       rewrite_clause_with_constraint_4(N,L,[],Vss3),
  1663.       append(Vss1,Vss3,Vss4),
  1664.       rewrite_clause_with_constraint_1(C,Vss4,Vss2).
  1665.     rewrite_clause_with_constraint_2(T,Vss) :-
  1666.       (var(T) -> (
  1667.         Vss=[]
  1668.       ); (
  1669.         vars_tail(T,V),
  1670.         (not(not((
  1671.             const_list(V),
  1672.             (rwr((T->_),var(V1,V1,_),_,_); rwr(T=_,var(V1,V1,_),_,_))))) -> (
  1673.           list_vars(T,Vs),
  1674.           (Vs=[_] -> (
  1675.             Vss=[]
  1676.           ); (
  1677.             Vss=[Vs]
  1678.           ))
  1679.         ); (
  1680.           functor(T,_,N),
  1681.           rewrite_clause_with_constraint_3(N,T,[],Vss)
  1682.         ))
  1683.       )).
  1684.     rewrite_clause_with_constraint_3(0,_,Vss,Vss) :- !.
  1685.     rewrite_clause_with_constraint_3(N,T,Vss1,Vss2) :-
  1686.       functor(T,_,N1),
  1687.       N2 is N1-N+1,
  1688.       arg(N2,T,A),
  1689.       rewrite_clause_with_constraint_2(A,Vss3),
  1690.       append(Vss1,Vss3,Vss4),
  1691.       N3 is N-1,
  1692.       rewrite_clause_with_constraint_3(N3,T,Vss4,Vss2).
  1693.     rewrite_clause_with_constraint_4(0,_,Vss,Vss) :- !.
  1694.     rewrite_clause_with_constraint_4(N,L,Vss1,Vss2) :-
  1695.       functor(L,_,N1),
  1696.       N2 is N1-N+1,
  1697.       arg(N2,L,Li),
  1698.       N3 is N-1,
  1699.       (var(Li) -> (
  1700.         rewrite_clause_with_constraint_4(N3,L,Vss1,Vss2)
  1701.       ); (
  1702.         rewrite_clause_with_constraint_2(Li,Vss3),
  1703.         append(Vss1,Vss3,Vss4),
  1704.         rewrite_clause_with_constraint_4(N3,L,Vss4,Vss2)
  1705.       )).
  1706.     rewrite_clause_with_constraint_5([],[]) :- !.
  1707.     rewrite_clause_with_constraint_5([Vs|Vss],[Cons|Conss]) :-
  1708.       (Vs==[] -> (
  1709.         Cons=[]
  1710.       ); (
  1711.         permutations(Vs,P),
  1712.         bagof(Con1,rewrite_clause_with_constraint_6(P,Con1),Cons)
  1713.       )),
  1714.       rewrite_clause_with_constraint_5(Vss,Conss).
  1715.     rewrite_clause_with_constraint_6(P,Con) :-
  1716.       member(L,P),
  1717.       rewrite_clause_with_constraint_7(L,Con).
  1718.     rewrite_clause_with_constraint_7([_],[]) :- !.
  1719.     rewrite_clause_with_constraint_7([X,Y|Z],[X>Y|Con]) :-
  1720.       rewrite_clause_with_constraint_7([Y|Z],Con).
  1721.     rewrite_clause_with_constraint_8([],Con1,Con2) :-
  1722.       identical_delete_all_duplicates(Con1,Con2).
  1723.     rewrite_clause_with_constraint_8([Cons|Conss],Con1,Con2) :-
  1724.       (Cons==[] -> (
  1725.         rewrite_clause_with_constraint_8(Conss,Con1,Con2)
  1726.       ); (
  1727.         member(Con3,Cons),
  1728.         append(Con1,Con3,Con4),
  1729.         rewrite_clause_with_constraint_8(Conss,Con4,Con2)
  1730.       )).
  1731.     rewrite_clause_with_constraint_9([],_,_,_,_,Cts,Cts) :- !.
  1732.     rewrite_clause_with_constraint_9([Con1|Cons],C,Sp,Ds,Con2,Cts1,Cts2) :-
  1733.       (Con2==[] -> (
  1734.         (constraint_consistent(Con1) -> (
  1735.           rewrite_clause_with_constraint_10(C,Sp,Ds,Con1,Cts3)
  1736.         ); (
  1737.           Cts3=[]
  1738.         ))
  1739.       ); (
  1740.         append(Con2,Con1,Con3),
  1741.         identical_delete_all_duplicates(Con3,Con4),
  1742.         (constraint_consistent(Con4) -> (
  1743.           rewrite_clause_with_constraint_10(C,Sp,Ds,Con4,Cts3)
  1744.         ); (
  1745.           Cts3=[]
  1746.         ))
  1747.       )),
  1748.       append(Cts1,Cts3,Cts4),
  1749.       rewrite_clause_with_constraint_9(Cons,C,Sp,Ds,Con2,Cts4,Cts2).
  1750.     rewrite_clause_with_constraint_10([],_,_,Con,[[[],0,Con]]) :- !.
  1751.     rewrite_clause_with_constraint_10([L|Ls],Sp,Ds,Con,Cts) :-
  1752.       rewrite_lit_with_constraint(L,Sp,Ds,Con,Cts1),
  1753.       rewrite_clause_with_constraint_11(Cts1,Ls,Sp,Ds,[],Cts).
  1754.     rewrite_clause_with_constraint_11([],_,_,_,Cts,Cts) :- !.
  1755.     rewrite_clause_with_constraint_11([[L,0,Con]|Cts1],Ls,Sp,Ds,Cts2,Cts3) :-
  1756.       !,
  1757.       rewrite_clause_with_constraint_10(Ls,Sp,Ds,Con,Cts4),
  1758.       rewrite_clause_with_constraint_12(Cts4,L,Cts5),
  1759.       append(Cts2,Cts5,Cts6),
  1760.       rewrite_clause_with_constraint_11(Cts1,Ls,Sp,Ds,Cts6,Cts3).
  1761.     rewrite_clause_with_constraint_11([[L,1,Con]|Cts1],Ls,Sp,Ds,Cts2,Cts3) :-
  1762.       append(Cts2,[[[L|Ls],1,Con]],Cts4),
  1763.       rewrite_clause_with_constraint_11(Cts1,Ls,Sp,Ds,Cts4,Cts3).
  1764.     rewrite_clause_with_constraint_12([],_,[]) :- !.
  1765.     rewrite_clause_with_constraint_12([[L1,F,Con]|Cts1],L2,
  1766.         [[[L2|L1],F,Con]|Cts2]) :-
  1767.       rewrite_clause_with_constraint_12(Cts1,L2,Cts2).
  1768.  
  1769. %%%
  1770. %%% rewrite_lit_with_constraint(L,Sp,Ds,Con,Cts)
  1771. %%%   input
  1772. %%%     L   : literal
  1773. %%%     Sp  : support data for computing priority
  1774. %%%     Ds  : distance data for computing priority
  1775. %%%     Con : constraint
  1776. %%%   output
  1777. %%%     Cts : set of simplified literal/flag/constraint triples obtained by
  1778. %%%           rewriting L under the constraint Con.  Flag is set as follows:
  1779. %%%             0 -- literal fully rewritten
  1780. %%%             1 -- rewrite terminated early due to priority
  1781. %%%   notes
  1782. %%%     If Sp is uninstantiated, the priority is not checked after using a
  1783. %%%       marked equational rule.
  1784. %%%
  1785.     rewrite_lit_with_constraint(S=T,Sp,Ds,Con,Cts) :-
  1786.       restricted_rewrite,
  1787.       !,
  1788.       order_term(S,O,T),
  1789.       (O=='>' -> (
  1790.         rewrite_term_with_constraint(T,Sp,Ds,Con,Cts1),
  1791.         rewrite_lit_with_constraint_1(Cts1,S,Sp,Ds,[],Cts2)
  1792.       ); (O=='<' -> (
  1793.         rewrite_term_with_constraint(S,Sp,Ds,Con,Cts1),
  1794.         rewrite_lit_with_constraint_1(Cts1,T,Sp,Ds,[],Cts2)
  1795.       ); (
  1796.         rewrite_term_with_constraint(S,Sp,Ds,Con,Cts1),
  1797.         rewrite_lit_with_constraint_6(Cts1,T,Sp,Ds,[],Cts2)
  1798.       ))),
  1799.       rewrite_lit_with_constraint_7(Cts2,Cts).
  1800.     rewrite_lit_with_constraint(S=T,Sp,Ds,Con,Cts) :-
  1801.       !,
  1802.       remove_rewrite_rule(S=T),
  1803.       rewrite_term_with_constraint(S,Sp,Ds,Con,Cts1),
  1804.       restore_rewrite_rule(S=T),
  1805.       rewrite_lit_with_constraint_9(Cts1,S,T,Sp,Ds,[],Cts2),
  1806.       rewrite_lit_with_constraint_7(Cts2,Cts).
  1807.     rewrite_lit_with_constraint(not S=T,Sp,Ds,Con,Cts) :-
  1808.       restricted_rewrite,
  1809.       !,
  1810.       rewrite_term_args_only_with_constraint(S,Sp,Ds,Con,Cts1),
  1811.       rewrite_lit_with_constraint_6(Cts1,T,Sp,Ds,[],Cts2),
  1812.       rewrite_lit_with_constraint_12(Cts2,Cts).
  1813.     rewrite_lit_with_constraint(not L,Sp,Ds,Con,Cts) :-
  1814.       !,
  1815.       rewrite_term_with_constraint(L,Sp,Ds,Con,Cts1),
  1816.       rewrite_lit_with_constraint_12(Cts1,Cts).
  1817.     rewrite_lit_with_constraint(L,Sp,Ds,Con,Cts) :-
  1818.       rewrite_term_with_constraint(L,Sp,Ds,Con,Cts).
  1819.     rewrite_lit_with_constraint_1([],_,_,_,Cts,Cts) :- !.
  1820.     rewrite_lit_with_constraint_1([[T,0,Con]|Cts1],S,Sp,Ds,Cts2,Cts3) :-
  1821.       vars_tail(S,V),
  1822.       rewrite_lit_with_constraint_2(S,V,S1),
  1823.       order_term(S1,<,T),
  1824.       !,
  1825.       rewrite_term_with_constraint(S1,Sp,Ds,Con,Cts4),
  1826.       rewrite_lit_with_constraint_3(Cts4,T,Cts5),
  1827.       append(Cts2,Cts5,Cts6),
  1828.       rewrite_lit_with_constraint_1(Cts1,S,Sp,Ds,Cts6,Cts3).
  1829.     rewrite_lit_with_constraint_1([[T,0,Con]|Cts1],S,Sp,Ds,Cts2,Cts3) :-
  1830.       !,
  1831.       rewrite_term_args_only_with_constraint(S,Sp,Ds,Con,Cts4),
  1832.       rewrite_lit_with_constraint_4(Cts4,S,T,Sp,Ds,[],Cts5),
  1833.       append(Cts2,Cts5,Cts6),
  1834.       rewrite_lit_with_constraint_1(Cts1,S,Sp,Ds,Cts6,Cts3).
  1835.     rewrite_lit_with_constraint_1([[T,1,Con]|Cts1],S,Sp,Ds,Cts2,Cts3) :-
  1836.       append(Cts2,[[S=T,1,Con]],Cts4),
  1837.       rewrite_lit_with_constraint_1(Cts1,S,Sp,Ds,Cts4,Cts3).
  1838.     rewrite_lit_with_constraint_2(S1,V,S2) :-
  1839.       rwr((S1->S2),var(V1,V2,_),_,_),
  1840.       unify_lists(V1,V2),
  1841.       not(not(const_list(V))).
  1842.     rewrite_lit_with_constraint_2(S1,V,S2) :-
  1843.       equational_rewrite,
  1844.       rwr(S1=S2,var(V1,V2,_),_,_),
  1845.       unify_lists(V1,V2),
  1846.       not(not(const_list(V))),
  1847.       order_term(S1,>,S2).
  1848.     rewrite_lit_with_constraint_3([],_,[]) :- !.
  1849.     rewrite_lit_with_constraint_3([[S,F,Con]|Cts1],T,[[S=T,F,Con]|Cts2]) :-
  1850.       rewrite_lit_with_constraint_3(Cts1,T,Cts2).
  1851.     rewrite_lit_with_constraint_4([],_,_,_,_,Cts,Cts) :- !.
  1852.     rewrite_lit_with_constraint_4([[S1,0,Con]|Cts1],S2,T,Sp,Ds,Cts2,Cts3) :-
  1853.       S1==S2,
  1854.       !,
  1855.       append(Cts2,[[S2=T,0,Con]],Cts4),
  1856.       rewrite_lit_with_constraint_4(Cts1,S2,T,Sp,Ds,Cts4,Cts3).
  1857.     rewrite_lit_with_constraint_4([[S1,0,Con]|Cts1],S2,T,Sp,Ds,Cts2,Cts3) :-
  1858.       !,
  1859.       rewrite_term_with_constraint(S1,Sp,Ds,Con,Cts4),
  1860.       rewrite_lit_with_constraint_5(Cts4,T,Cts5),
  1861.       append(Cts2,Cts5,Cts6),
  1862.       rewrite_lit_with_constraint_4(Cts1,S2,T,Sp,Ds,Cts6,Cts3).
  1863.     rewrite_lit_with_constraint_4([[S1,1,Con]|Cts1],S2,T,Sp,Ds,Cts2,Cts3) :-
  1864.       append(Cts2,[[S1=T,1,Con]],Cts4),
  1865.       rewrite_lit_with_constraint_4(Cts1,S2,T,Sp,Ds,Cts4,Cts3).
  1866.     rewrite_lit_with_constraint_5([],_,[]) :- !.
  1867.     rewrite_lit_with_constraint_5([[S,F,Con]|Cts1],T,[[S=T,F,Con]|Cts2]) :-
  1868.       rewrite_lit_with_constraint_5(Cts1,T,Cts2).
  1869.     rewrite_lit_with_constraint_6([],_,_,_,Cts,Cts) :- !.
  1870.     rewrite_lit_with_constraint_6([[S,0,Con]|Cts1],T,Sp,Ds,Cts2,Cts3) :-
  1871.       !,
  1872.       rewrite_term_with_constraint(T,Sp,Ds,Con,Cts4),
  1873.       rewrite_lit_with_constraint_8(Cts4,S,Cts5),
  1874.       append(Cts2,Cts5,Cts6),
  1875.       rewrite_lit_with_constraint_6(Cts1,T,Sp,Ds,Cts6,Cts3).
  1876.     rewrite_lit_with_constraint_6([[S,1,Con]|Cts1],T,Sp,Ds,Cts2,Cts3) :-
  1877.       append(Cts2,[[S=T,1,Con]],Cts4),
  1878.       rewrite_lit_with_constraint_6(Cts1,T,Sp,Ds,Cts4,Cts3).
  1879.     rewrite_lit_with_constraint_7([],[]) :- !.
  1880.     rewrite_lit_with_constraint_7([[S=T,F,Con]|Cts1],[[E,F,Con]|Cts2]) :-
  1881.       order_term(S,O,T),
  1882.       (O=='<' -> (
  1883.         E=(T=S)
  1884.       ); (
  1885.         E=(S=T)
  1886.       )),
  1887.       rewrite_lit_with_constraint_7(Cts1,Cts2).
  1888.     rewrite_lit_with_constraint_8([],_,[]) :- !.
  1889.     rewrite_lit_with_constraint_8([[T,F,Con]|Cts1],S,[[S=T,F,Con]|Cts2]) :-
  1890.       rewrite_lit_with_constraint_8(Cts1,S,Cts2).
  1891.     rewrite_lit_with_constraint_9([],_,_,_,_,Cts,Cts) :- !.
  1892.     rewrite_lit_with_constraint_9([[S1,0,Con]|Cts1],S2,T,Sp,Ds,Cts2,Cts3) :-
  1893.       !,
  1894.       (S1\==S2 -> (
  1895.         rewrite_term_with_constraint(T,Sp,Ds,Con,Cts4),
  1896.         rewrite_lit_with_constraint_8(Cts4,S1,Cts5),
  1897.         append(Cts2,Cts5,Cts6),
  1898.         rewrite_lit_with_constraint_9(Cts1,S2,T,Sp,Ds,Cts6,Cts3)
  1899.       ); (
  1900.         remove_rewrite_rule(T=S2),
  1901.         rewrite_term_with_constraint(T,Sp,Ds,Con,Cts4),
  1902.         restore_rewrite_rule(T=S2),
  1903.         rewrite_lit_with_constraint_8(Cts4,S1,Cts5),
  1904.         rewrite_lit_with_constraint_10(Cts5,T,Sp,Ds,[],Cts6),
  1905.         append(Cts2,Cts6,Cts7),
  1906.         rewrite_lit_with_constraint_9(Cts1,S2,T,Sp,Ds,Cts7,Cts3)
  1907.       )).
  1908.     rewrite_lit_with_constraint_9([[S1,1,Con]|Cts1],S2,T,Sp,Ds,Cts2,Cts3) :-
  1909.       append(Cts2,[[S1=T,1,Con]],Cts4),
  1910.       rewrite_lit_with_constraint_9(Cts1,S2,T,Sp,Ds,Cts4,Cts3).
  1911.     rewrite_lit_with_constraint_10([],_,_,_,Cts,Cts) :- !.
  1912.     rewrite_lit_with_constraint_10([[S=T1,0,Con]|Cts1],T2,Sp,Ds,Cts2,Cts3) :-
  1913.       T1\==T2,
  1914.       !,
  1915.       rewrite_term_with_constraint(S,Sp,Ds,Con,Cts4),
  1916.       rewrite_lit_with_constraint_11(Cts4,T1,Cts5),
  1917.       append(Cts2,Cts5,Cts6),
  1918.       rewrite_lit_with_constraint_10(Cts1,T2,Sp,Ds,Cts6,Cts3).
  1919.     rewrite_lit_with_constraint_10([Ct|Cts1],T,Sp,Ds,Cts2,Cts3) :-
  1920.       append(Cts2,[Ct],Cts4),
  1921.       rewrite_lit_with_constraint_10(Cts1,T,Sp,Ds,Cts4,Cts3).
  1922.     rewrite_lit_with_constraint_11([],_,[]) :- !.
  1923.     rewrite_lit_with_constraint_11([[S,F,Con]|Cts1],T,[[S=T,F,Con]|Cts2]) :-
  1924.       rewrite_lit_with_constraint_11(Cts1,T,Cts2).
  1925.     rewrite_lit_with_constraint_12([],[]) :- !.
  1926.     rewrite_lit_with_constraint_12([[L,F,Con]|Cts1],
  1927.         [[not L,F,Con]|Cts2]) :-
  1928.       rewrite_lit_with_constraint_12(Cts1,Cts2).
  1929.  
  1930. %%%
  1931. %%% rewrite_term_with_constraint(T,Sp,Ds,Con,Cts)
  1932. %%%   input
  1933. %%%     T   : term
  1934. %%%     Sp  : support data for computing priority
  1935. %%%     Ds  : distance data for computing priority
  1936. %%%     Con : constraint
  1937. %%%   output
  1938. %%%     Cts : set of simplified term/flag/constraint triples obtained by
  1939. %%%           rewriting T under the constraint C.  Flag is set as follows:
  1940. %%%             0 -- term fully rewritten
  1941. %%%             1 -- rewrite terminated early due to priority
  1942. %%%   notes
  1943. %%%     If Sp is uninstantiated, the priority is not checked after using a
  1944. %%%       marked equational rule.
  1945. %%%
  1946.     rewrite_term_with_constraint(T,Sp,Ds,Con,Cts) :-
  1947.       (var(T) -> (
  1948.         Cts=[[T,0,Con]]
  1949.       ); (
  1950.         abolish(term_rewritten,0),
  1951.         vars_tail({T,Con},V),
  1952.         rewrite_term_with_constraint_1(T,Sp,Ds,V,Con,Cts)
  1953.       )).
  1954.     rewrite_term_with_constraint_1(T,Sp,Ds,V,Con,Cts) :-
  1955. %      not(not((
  1956. %        var_list(V),
  1957. %        write('*** entered rewrite_term_with_constraint_1('),
  1958. %        write(T),
  1959. %        write(','),
  1960. %        (var(Sp) -> (
  1961. %          write('Sp')
  1962. %        ); (
  1963. %          write(Sp)
  1964. %        )),
  1965. %        write(','),
  1966. %        (var(Ds) -> (
  1967. %          write('Ds')
  1968. %        ); (
  1969. %          write(Ds)
  1970. %        )),
  1971. %        write(',V,'),
  1972. %        write(Con),
  1973. %        write(',Cts)'),
  1974. %        write_line(0,'')
  1975. %      ))),
  1976.       (var(T)  -> (
  1977.         Cts=[[T,0,Con]]
  1978.       ); (
  1979.         term_size_structure(T,TSize),
  1980.         term_size(Con,Size2),
  1981.         ((term_rewritten; (not(not((const_list(V),
  1982.             not(dont_use_rewrite_rule_outer(T=_,V))))))) -> (
  1983.           arg(1,TSize,Size1),
  1984.           Size3 is (Size1*10000)+Size2,
  1985.           ((cached_rewrite(Size3,T,Con,var(V1,V2,V3),Cts),
  1986.               not(not(const_list(V))),
  1987.               not(not(const_list(V3))),
  1988.               not(not((V1=V2,const_list(V))))) -> (
  1989.             assert(rewrite_found)
  1990.           ); (
  1991.             Caching_allowed=1
  1992.           ))
  1993.         ); (
  1994.           Caching_allowed=0
  1995.        )),
  1996.         (rewrite_found -> (
  1997.           abolish(rewrite_found,0)
  1998.         ); (
  1999.           (not(term_rewritten) -> (
  2000.             Term_not_rewritten=1
  2001.           ); (
  2002.             Term_not_rewritten=0
  2003.           )),
  2004.           rewrite_term_with_constraint_2(T,Sp,Ds,V,Con,TSize,Size2,Cts1),
  2005.          ((Cts1\==[[T,0,Con]], Cts1\==[[T,1,Con]]) -> (
  2006.             assert_once(term_rewritten)
  2007.           ); true),
  2008.           (rtwc_stop -> (
  2009.             abolish(rtwc_stop,0),
  2010.             Cts1=[[T1,F,Con1]],
  2011.             (F==0 -> (
  2012.              rewrite_term_with_constraint_1(T1,Sp,Ds,V,Con1,Cts)
  2013.             ); (
  2014.               Cts=Cts1
  2015.             ))
  2016.           ); (
  2017.             (Cts1==[] -> (
  2018.               Cts=[[T,0,Con]]
  2019.             ); (
  2020.               simplify_constraint_triples(Cts1,Cts2),
  2021.               rewrite_term_with_constraint_7(Cts2,Con,Cons1),
  2022.               negate_constraints(Cons1,Cons2),
  2023.               (Con==[] -> (
  2024.                 Cons3=Cons2
  2025.               ); (
  2026.                rewrite_term_with_constraint_8(Cons2,Con,Cons3)
  2027.               )),
  2028.               rewrite_term_with_constraint_9(Cons3,T,Cts3),
  2029.               ((term_rewritten, Term_not_rewritten==1) -> (
  2030.                 rewrite_term_with_constraint_10(Cts2,Sp,Ds,V,[],Cts4),
  2031.                 abolish(term_rewritten,0),
  2032.                 rewrite_term_with_constraint_10(Cts3,Sp,Ds,V,[],Cts5),
  2033.                 assert(term_rewritten),
  2034.                 append(Cts4,Cts5,Cts)
  2035.               ); (
  2036.                 append(Cts2,Cts3,Cts4),
  2037.                 rewrite_term_with_constraint_10(Cts4,Sp,Ds,V,[],Cts)
  2038.               ))
  2039.             ))
  2040.           )),
  2041.           (Caching_allowed==1 -> (
  2042.             counter(cached_rewrite_count,N),
  2043.             cache_size(CacheSize),
  2044.             (N > CacheSize -> (
  2045.               write_line(0,'*** cleared cached_rewrite: ',N),
  2046.               set_counter(cached_rewrite_count,0),
  2047.               abolish(cached_rewrite,5)
  2048.             ); true),
  2049.             term_size(Cts,Size4),
  2050.             Size5 is Size1+Size2+Size4,
  2051.             increment_counter(cached_rewrite_count,Size5),
  2052.             linearize_term({T,Con},{T2,Con2},V4,V5),
  2053.             assert(cached_rewrite(Size3,T2,Con2,var(V4,V5,V),Cts))
  2054.           ); true)
  2055.         ))
  2056.       )).
  2057.     rewrite_term_with_constraint_2(T,Sp,Ds,V,Con,TSize,Size2,Cts) :-
  2058. %      not(not((
  2059. %        var_list(V),
  2060. %        write('*** entered rewrite_term_with_constraint_2('),
  2061. %        write(T),
  2062. %        write(','),
  2063. %        (var(Sp) -> (
  2064. %          write('Sp')
  2065. %        ); (
  2066. %          write(Sp)
  2067. %        )),
  2068. %        write(','),
  2069. %        (var(Ds) -> (
  2070. %          write('Ds')
  2071. %        ); (
  2072. %          write(Ds)
  2073. %        )),
  2074. %        write(',V,'),
  2075. %        write(Con),
  2076. %        write(','),
  2077. %        write(TSize),
  2078. %        write(','),
  2079. %        write(Size2),
  2080. %        write(',Cts)'),
  2081. %        write_line(0,'')
  2082. %      ))),
  2083.       (var(T) -> (
  2084.         Cts=[]
  2085.       ); (
  2086.         ((term_rewritten; (not(not((const_list(V),
  2087.             not(dont_use_rewrite_rule_outer(T=_,V))))))) -> (
  2088.           arg(1,TSize,Size1),
  2089.           Size3 is (Size1*10000)+Size2,
  2090.           ((cached_subterm_rewrite(Size3,T,Con,var(V1,V2,V3),Cts,Stop),
  2091.               not(not(const_list(V))),
  2092.               not(not(const_list(V3))),
  2093.               not(not((V1=V2,const_list(V))))) -> (
  2094.             assert(subterm_rewrite_found),
  2095.             (Stop==1 -> (
  2096.               assert_once(rtwc_stop)
  2097.             ); true)
  2098.           ); (
  2099.             Caching_allowed=1
  2100.           ))
  2101.         ); (
  2102.           Caching_allowed=0
  2103.         )),
  2104.         (subterm_rewrite_found -> (
  2105.           abolish(subterm_rewrite_found,0)
  2106.         ); (
  2107.           bagof1(R,rewrite_term_with_constraint_3(T,V,R),Rs),
  2108.           rewrite_term_with_constraint_4(Rs,Sp,Ds,Con,Cts1),
  2109.           (rtwc_stop -> (
  2110.             tail(Cts1,Ct),
  2111.             Cts=[Ct]
  2112.           ); (
  2113.             functor(T,_,N1),
  2114.             rewrite_term_with_constraint_5(N1,T,Sp,Ds,V,Con,TSize,Size2,[],
  2115.               Cts2),
  2116.             (rtwc_stop -> (
  2117.               Cts=Cts2
  2118.             ); (
  2119.               append(Cts1,Cts2,Cts)
  2120.             ))
  2121.           )),
  2122.           (Caching_allowed==1 -> (
  2123.             counter(cached_subterm_rewrite_count,N2),
  2124.             cache_size(CacheSize),
  2125.             (N2 > CacheSize -> (
  2126.               write_line(0,'*** cleared cached_subterm_rewrite: ',N2),
  2127.               set_counter(cached_subterm_rewrite_count,0),
  2128.               abolish(cached_subterm_rewrite,6)
  2129.             ); true),
  2130.             term_size(Cts,Size4),
  2131.             Size5 is Size1+Size2+Size4,
  2132.             increment_counter(cached_subterm_rewrite_count,Size5),
  2133.             linearize_term({T,Con},{T1,Con1},V4,V5),
  2134.             (rtwc_stop -> (
  2135.               assert(cached_subterm_rewrite(Size3,T1,Con1,var(V4,V5,V),Cts,1))
  2136.             ); (
  2137.               assert(cached_subterm_rewrite(Size3,T1,Con1,var(V4,V5,V),Cts,0))
  2138.             ))
  2139.           ); true)
  2140.         ))
  2141.       )).
  2142.     rewrite_term_with_constraint_3(T,V,{(T->T1),F}) :-
  2143.       rwr((T->T1),var(V1,V2,V3),F,_),
  2144.       unify_lists(V1,V2),
  2145.       not(not(const_list(V))),
  2146.       (term_rewritten;
  2147.         not(const_list(V3));
  2148.         (not(not((
  2149.           const_list(V3),
  2150.           not(dont_use_rewrite_rule_outer(T=T1,V3))
  2151.         ))))
  2152.       ),
  2153.       !.
  2154.     rewrite_term_with_constraint_3(T,V,{T=T1,F}) :-
  2155.       rwr((T=T1),var(V1,V2,V3),F,_),
  2156.       unify_lists(V1,V2),
  2157.       not(not(const_list(V))),
  2158.       (term_rewritten;
  2159.         not(const_list(V3));
  2160.         (not(not((
  2161.           const_list(V3),
  2162.           not(dont_use_rewrite_rule_outer(T=T1,V3))
  2163.         ))))
  2164.       ).
  2165.     rewrite_term_with_constraint_4([],_,_,_,[]) :- !.
  2166.     rewrite_term_with_constraint_4([{(T->T1),F}|_],Sp,Ds,Con,[Ct]) :-
  2167.       !,
  2168.       assert(rtwc_stop),
  2169.       ((F==1, nonvar(Sp), priority_bound(PrioBound)) -> (
  2170.         calculate_priority_clause([T1],Sp,Ds,Pr),
  2171.         (check_prioritybound(Pr,PrioBound) -> (
  2172.           Ct=[T1,0,Con]
  2173.         ); (
  2174.           Ct=[T1,1,Con]
  2175.         ))
  2176.       ); (
  2177.         Ct=[T1,0,Con]
  2178.       )).
  2179.     rewrite_term_with_constraint_4([{T=T1,F}|Rs],Sp,Ds,Con,Cts) :-
  2180.       !,
  2181.       order_term(T,O,T1),
  2182.       ((O=='>'; (O=='!', implies_constraint(Con,[T>T1]))) -> (
  2183.         assert(rtwc_stop),
  2184.         ((F==1, nonvar(Sp), priority_bound(PrioBound)) -> (
  2185.           calculate_priority_clause([T1],Sp,Ds,Pr),
  2186.           (check_prioritybound(Pr,PrioBound) -> (
  2187.             Cts=[[T1,0,Con]]
  2188.           ); (
  2189.             Cts=[[T1,1,Con]]
  2190.           ))
  2191.         ); (
  2192.           Cts=[[T1,0,Con]]
  2193.         ))
  2194.       ); (
  2195.         ((not(precompute_constraints), O=='!',
  2196.             constraint_consistent([T>T1|Con])) -> (
  2197.           append(Con,[T>T1],Con1),
  2198.           ((F==1, nonvar(Sp), priority_bound(PrioBound)) -> (
  2199.             calculate_priority_clause([T1],Sp,Ds,Pr),
  2200.             (check_prioritybound(Pr,PrioBound) -> (
  2201.               Ct=[T1,0,Con1]
  2202.             ); (
  2203.               Ct=[T1,1,Con1]
  2204.             ))
  2205.           ); (
  2206.             Ct=[T1,0,Con1]
  2207.           )),
  2208.           rewrite_term_with_constraint_4(Rs,Sp,Ds,Con,Cts1),
  2209.           Cts=[Ct|Cts1]
  2210.         ); (
  2211.           rewrite_term_with_constraint_4(Rs,Sp,Ds,Con,Cts)
  2212.         ))
  2213.       )).
  2214.     rewrite_term_with_constraint_5(0,_,_,_,_,_,_,_,Cts,Cts) :- !.
  2215.     rewrite_term_with_constraint_5(N,T,Sp,Ds,V,Con,TSize,Size2,Cts1,Cts2) :-
  2216.       arg(N,T,Ti),
  2217.       N1 is N+1,
  2218.       arg(N1,TSize,TiSize),
  2219.       rewrite_term_with_constraint_2(Ti,Sp,Ds,V,Con,TiSize,Size2,Cts3),
  2220.       rewrite_term_with_constraint_6(Cts3,T,N,Cts4),
  2221.       (rtwc_stop -> (
  2222.         Cts2=Cts4
  2223.       ); (
  2224.         append(Cts1,Cts4,Cts5),
  2225.         N2 is N-1,
  2226.         rewrite_term_with_constraint_5(N2,T,Sp,Ds,V,Con,TSize,Size2,Cts5,Cts2)
  2227.       )).
  2228.     rewrite_term_with_constraint_6([],_,_,[]) :- !.
  2229.     rewrite_term_with_constraint_6([[Ti,F,Con]|Cts1],T1,N,[[T2,F,Con]|Cts2]) :-
  2230.       T1=..[Func|Ts1],
  2231.       replace_list(Ts1,N,Ti,Ts2),
  2232.       T2=..[Func|Ts2],
  2233.       rewrite_term_with_constraint_6(Cts1,T1,N,Cts2).
  2234.     rewrite_term_with_constraint_7([],_,[]) :- !.
  2235.     rewrite_term_with_constraint_7([[_,_,Con1]|Cts],Con2,[Con3|Cons]) :-
  2236.       identical_set_difference(Con1,Con2,Con3),
  2237.       rewrite_term_with_constraint_7(Cts,Con2,Cons).
  2238.     rewrite_term_with_constraint_8([],_,[]) :-!.
  2239.     rewrite_term_with_constraint_8([Con1|Cons1],Con2,[Con3|Cons2]) :-
  2240.       append(Con2,Con1,Con4),
  2241.       identical_delete_all_duplicates(Con4,Con3),
  2242.       constraint_consistent(Con3),
  2243.       !,
  2244.       rewrite_term_with_constraint_8(Cons1,Con2,Cons2).
  2245.     rewrite_term_with_constraint_8([_|Cons1],Con,Cons2) :-
  2246.       rewrite_term_with_constraint_8(Cons1,Con,Cons2).
  2247.     rewrite_term_with_constraint_9([],_,[]) :- !.
  2248.     rewrite_term_with_constraint_9([Con|Cons],T,[[T,0,Con]|Cts]) :-
  2249.       rewrite_term_with_constraint_9(Cons,T,Cts).
  2250.     rewrite_term_with_constraint_10([],_,_,_,Cts,Cts) :- !.
  2251.     rewrite_term_with_constraint_10([[T,0,Con]|Cts1],Sp,Ds,V,Cts2,Cts3) :-
  2252.       !,
  2253.       rewrite_term_with_constraint_1(T,Sp,Ds,V,Con,Cts4),
  2254.       append(Cts2,Cts4,Cts5),
  2255.       rewrite_term_with_constraint_10(Cts1,Sp,Ds,V,Cts5,Cts3).
  2256.     rewrite_term_with_constraint_10([[T,1,Con]|Cts1],Sp,Ds,V,Cts2,Cts3) :-
  2257.       append(Cts2,[[T,1,Con]],Cts4),
  2258.       rewrite_term_with_constraint_10(Cts1,Sp,Ds,V,Cts4,Cts3).
  2259.  
  2260. %%%
  2261. %%% rewrite_term_args_only_with_constraint(T,Sp,Ds,Con,Cts)
  2262. %%%   input
  2263. %%%     T   : term
  2264. %%%     Sp  : support data for computing priority
  2265. %%%     Ds  : distance data for computing priority
  2266. %%%     Con : constraint
  2267. %%%   output
  2268. %%%     Cts : set of simplified term/flag/constraint triples obtained by
  2269. %%%           rewriting the arguments of T under the constraint C.  Flag is
  2270. %%%           set as follows:
  2271. %%%             0 -- term fully rewritten
  2272. %%%             1 -- rewrite terminated early due to priority
  2273. %%%   notes
  2274. %%%     If Sp is uninstantiated, the priority is not checked after using a
  2275. %%%       marked equational rule.
  2276. %%%
  2277.     rewrite_term_args_only_with_constraint(T,_,_,Con,[[T,0,Con]]) :-
  2278.       var(T),
  2279.       !.
  2280.     rewrite_term_args_only_with_constraint(T,Sp,Ds,Con,Cts) :-
  2281.       functor(T,F,N),
  2282.       rewrite_term_args_only_with_constraint_1(N,T,Sp,Ds,Con,Cts1),
  2283.       rewrite_term_args_only_with_constraint_4(Cts1,F,Cts).
  2284.     rewrite_term_args_only_with_constraint_1(0,_,_,_,Con,[[[],0,Con]]) :- !.
  2285.     rewrite_term_args_only_with_constraint_1(N,T,Sp,Ds,Con,Cts) :-
  2286.       functor(T,_,N1),
  2287.       N2 is N1-N+1,
  2288.       arg(N2,T,Ti),
  2289.       rewrite_term_with_constraint(Ti,Sp,Ds,Con,Cts1),
  2290.       N3 is N-1,
  2291.       rewrite_term_args_only_with_constraint_2(Cts1,N3,T,Sp,Ds,[],Cts).
  2292.     rewrite_term_args_only_with_constraint_2([],_,_,_,_,Cts,Cts) :- !.
  2293.     rewrite_term_args_only_with_constraint_2([[Ti,0,Con]|Cts1],N,T,Sp,Ds,Cts2,
  2294.         Cts3) :-
  2295.       !,
  2296.       rewrite_term_args_only_with_constraint_1(N,T,Sp,Ds,Con,Cts4),
  2297.       rewrite_term_args_only_with_constraint_3(Cts4,Ti,Cts5),
  2298.       append(Cts2,Cts5,Cts6),
  2299.       rewrite_term_args_only_with_constraint_2(Cts1,N,T,Sp,Ds,Cts6,Cts3).
  2300.     rewrite_term_args_only_with_constraint_2([[Ti,1,Con]|Cts1],N,T,Sp,Ds,Cts2,
  2301.         Cts3) :-
  2302.       append(Cts2,[[[Ti|Ts],1,Con]],Cts4),
  2303.       rewrite_term_args_only_with_constraint_2(Cts1,N,T,Sp,Ds,Cts4,Cts3).
  2304.     rewrite_term_args_only_with_constraint_3([],_,[]) :- !.
  2305.     rewrite_term_args_only_with_constraint_3([[Ts,F,Con]|Cts1],Ti,
  2306.         [[[Ti|Ts],F,Con]|Cts2]) :-
  2307.       rewrite_term_args_only_with_constraint_3(Cts1,Ti,Cts2).
  2308.     rewrite_term_args_only_with_constraint_4([],_,[]) :- !.
  2309.     rewrite_term_args_only_with_constraint_4([[Ts,F,Con]|Cts1],Func,
  2310.         [[T,F,Con]|Cts2]) :-
  2311.       T=..[Func|Ts],
  2312.       rewrite_term_args_only_with_constraint_4(Cts1,Func,Cts2).
  2313.  
  2314. %%%
  2315. %%% simplify_constraint_triples(Cts1,Cts2)
  2316. %%%   input
  2317. %%%     Cts1 : set of constraint triples
  2318. %%%   output
  2319. %%%     Cts2 : Cts1 with redundant constraint triples deleted.  A constraint
  2320. %%%            triple [T1,F1,Con1] is redundant if there exist another
  2321. %%%            constraint triple [T2,F2,Con2] such that Con1 implies Con2.
  2322. %%%
  2323.     simplify_constraint_triples(Cts1,Cts2) :-
  2324.       identical_delete_all_duplicates(Cts1,Cts3),
  2325.       simplify_constraint_triples_1(Cts3,Cts3,Cts2).
  2326.     simplify_constraint_triples_1([],Cts,Cts) :- !.
  2327.     simplify_constraint_triples_1([Ct|Cts1],Cts2,Cts3) :-
  2328.       (simplify_constraint_triples_2(Ct,Cts2) -> (
  2329.         identical_delete_all(Ct,Cts2,Cts4),
  2330.         simplify_constraint_triples_1(Cts1,Cts4,Cts3)
  2331.       ); simplify_constraint_triples_1(Cts1,Cts2,Cts3)).
  2332.     simplify_constraint_triples_2([T1,F1,Con1],[[T2,F2,Con2]|Cts]) :-
  2333.       (([T1,F1,Con1]\==[T2,F2,Con2], implies_constraint(Con1,Con2)) -> true; (
  2334.         simplify_constraint_triples_2([T1,F1,Con1],Cts)
  2335.       )).
  2336.  
  2337. %%%
  2338. %%% implies_constraint(Con1,Con2)
  2339. %%%   input
  2340. %%%     Con1 : consistent constraint
  2341. %%%     Con2 : consistent constraint
  2342. %%%   exceptions
  2343. %%%     fails if C1 does not imply C2
  2344.     implies_constraint(Con1,Con2) :-
  2345.       identical_set_difference(Con2,Con1,Con3),
  2346.       implies_constraint_1(Con3,Con1).
  2347.     implies_constraint_1([],_) :- !.
  2348.     implies_constraint_1([S>T|Con1],Con2) :-
  2349.       not(constraint_consistent([T>S|Con2])),
  2350.       implies_constraint_1(Con1,Con2).
  2351.  
  2352. %%%
  2353. %%% negate_constraints(Cons1,Cons2)
  2354. %%%   input
  2355. %%%     Cons1 : set of constraints
  2356. %%%   output
  2357. %%%     Cons2 : negation of Cons2
  2358. %%%
  2359.     negate_constraints(Cons1,Cons2) :-
  2360.       bagof1(Con,negate_constraints_1(Cons1,[],Con),Cons3),
  2361.       identical_delete_all_duplicates(Cons3,Cons4),
  2362.       negate_constraints_2(Cons4,Cons4,Cons2).
  2363.     negate_constraints_1([],Con,Con).
  2364.     negate_constraints_1([Con1|Cons],Con2,Con3) :-
  2365.       member(S>T,Con1),
  2366.       append(Con2,[T>S],Con4),
  2367.       constraint_consistent(Con4),
  2368.       negate_constraints_1(Cons,Con4,Con3).
  2369.     negate_constraints_2([],Cons,Cons) :- !.
  2370.     negate_constraints_2([Con|Cons1],Cons2,Cons3) :-
  2371.       (negate_constraints_3(Con,Cons2) -> (
  2372.         identical_delete_all(Con,Cons2,Cons4),
  2373.         negate_constraints_2(Cons1,Cons4,Cons3)
  2374.       ); negate_constraints_2(Cons1,Cons2,Cons3)).
  2375.     negate_constraints_3(Con1,[Con2|Cons]) :-
  2376.       ((Con1\==Con2, implies_constraint(Con1,Con2)) -> true; (
  2377.         negate_constraints_3(Con,Cons)
  2378.       )).
  2379.  
  2380. %%%
  2381. %%% Constraint_consistent(Con)
  2382. %%%   input
  2383. %%%     Con : constraint
  2384. %%%   exceptions
  2385. %%%     fails if constraint is not consistent
  2386. %%%
  2387.     constraint_consistent([]) :- !.
  2388.     constraint_consistent([S>T]) :-
  2389.      !,
  2390.       order_term(S,O,T),
  2391.       ((O=='>'; O=='!') -> true; fail).
  2392.     constraint_consistent(Con) :-
  2393.       vars_tail(Con,V),
  2394.       constraint_consistent_1(Con,V).
  2395.     constraint_consistent_1(Con,V) :-
  2396. %      not(not((
  2397. %        var_list(V),
  2398. %        write('*** entered constraint_consistent_1('),
  2399. %        write(Con),
  2400. %        write(',V)'),
  2401. %        write_line(0,'')
  2402. %      ))),
  2403.       term_size(Con,Size),
  2404.       ((not(not((
  2405.           const_list(V),
  2406.           cached_constraint_consistent(Size,Con,V,F),
  2407.           assert(ccc_flag(F)))))) -> (
  2408.         retract(ccc_flag(F)),
  2409.         (F==0 -> fail; true)
  2410.       ); (
  2411.         counter(cached_constraint_consistent_count,N),
  2412.         cache_size(CacheSize),
  2413.         (N > CacheSize -> (
  2414.           write_line(0,'*** cleared cached_constraint_consistent: ',N),
  2415.           set_counter(cached_constraint_consistent_count,0),
  2416.           abolish(cached_constraint_consistent,4)
  2417.         ); true),
  2418.         increment_counter(cached_constraint_consistent_count,Size),
  2419.         (not(not(constraint_consistent_2(Con,V))) -> (
  2420.           assert(cached_constraint_consistent(Size,Con,V,1))
  2421.         ); (
  2422.           assert(cached_constraint_consistent(Size,Con,V,0)),
  2423.           fail
  2424.         ))
  2425.       )).
  2426.     constraint_consistent_2(Con,V) :-
  2427.       constraint_consistent_3(Con),
  2428.       constraint_consistent_4(Con,[],Con1),
  2429.       (Con1==[] -> true; (
  2430.         ((member_N(Con1,S>T,Con2), nonvar(S), nonvar(T)) -> (
  2431.           order_term_with_constraint(S,T,Con3),
  2432.           append(Con2,Con3,Con4),
  2433.           identical_delete_all_duplicates(Con4,Con5),
  2434.           constraint_consistent_1(Con5,V)
  2435.         ); (
  2436.           (constraint_consistent_5(Con1,X) -> (
  2437.             bagof1(Cond,constraint_consistent_8(Con1,X,Cond),Con2),
  2438.             constraint_consistent_9(Con1,X,[],Con3),
  2439.             append(Con3,Con2,Con4),
  2440.             identical_delete_all_duplicates(Con4,Con5),
  2441.             constraint_consistent_1(Con5,V)
  2442.           ); fail)
  2443.         ))
  2444.       )).
  2445.     constraint_consistent_3([]) :- !.
  2446.     constraint_consistent_3([S>T|Con]) :-
  2447.       not(identical_member(T>S,Con)),
  2448.       constraint_consistent_3(Con).
  2449.     constraint_consistent_4([],Con,Con) :- !.
  2450.     constraint_consistent_4([S>T|Con1],Con2,Con3) :-
  2451.       (is_subterm(S,T) -> fail; (
  2452.         (is_subterm(T,S) -> (
  2453.           constraint_consistent_4(Con1,Con2,Con3)
  2454.         ); (
  2455.           append(Con2,[S>T],Con4),
  2456.           constraint_consistent_4(Con1,Con4,Con3)
  2457.         ))
  2458.       )).
  2459.     constraint_consistent_5(Con,X) :-
  2460.       list_vars(Con,Vs),
  2461.       constraint_consistent_6(Vs,Con,X).
  2462.     constraint_consistent_6([V|Vs],Con,X) :-
  2463.       (constraint_consistent_7(Con,V) -> (
  2464.         X=V
  2465.       ); (
  2466.         constraint_consistent_6(Vs,Con,X)
  2467.       )).
  2468.     constraint_consistent_7([],_) :- !.
  2469.     constraint_consistent_7([_>T|Con],V) :-
  2470.       (V==T; not(identical_embedded(V,T))),
  2471.       !,
  2472.       constraint_consistent_7(Con,V).
  2473.     constraint_consistent_8(Con1,X,S3>T2) :-
  2474.       member(S1>T1,Con1),
  2475.       T1==X,
  2476.       member(S2>T2,Con1),
  2477.       identical_embedded(X,S2),
  2478.       substitute_term_all(S2,X,S1,S3).
  2479.     constraint_consistent_9([],_,Con,Con) :- !.
  2480.     constraint_consistent_9([S>T|Con1],X,Con2,Con3) :-
  2481.       ((T==X; identical_embedded(X,S)) -> (
  2482.         constraint_consistent_9(Con1,X,Con2,Con3)
  2483.       ); (
  2484.         append(Con2,[S>T],Con4),
  2485.         constraint_consistent_9(Con1,X,Con4,Con3)
  2486.       )).
  2487.  
  2488. %%%
  2489. %%% Order_term_with_constraint(S,T,Con)
  2490. %%%   input
  2491. %%%     S   : term
  2492. %%%     T   : term
  2493. %%%   output
  2494. %%%     Con : consistent constraint under which S > T
  2495. %%%   exceptions
  2496. %%%     fails if not S > T under any consistent constraint
  2497. %%%   notes
  2498. %%%     additional consistent constraints under which S > T are returned on 
  2499. %%%       backtracking
  2500. %%%     order_term_with_constraint is only supported for lpo
  2501. %%%
  2502.     order_term_with_constraint(S,T,Con) :-
  2503.       (term_ordering(lpo) -> (
  2504.         order_term_lpo_with_constraint(S,T,Con)
  2505.       ); (
  2506.         (term_ordering(X) -> (
  2507.           write_line(5,'Invalid term ordering: ',X),
  2508.           abort
  2509.         ); (
  2510.           write_line(5,'No term ordering'),
  2511.           abort
  2512.         ))
  2513.       )).
  2514.  
  2515. %%%
  2516. %%% Order_term_lpo_with_constraint(S,T,Con)
  2517. %%%   input
  2518. %%%     S   : term
  2519. %%%     T   : term
  2520. %%%   output
  2521. %%%     Con : constraint consistent with respect to lpo under which S > T
  2522. %%%   exceptions
  2523. %%%     fails if not S > T under any constraint consistent with respect to lpo
  2524. %%%   notes
  2525. %%%     additional constraints consistent with respect to lpo under which
  2526. %%%       S > T are returned on backtracking
  2527. %%%
  2528.     order_term_lpo_with_constraint(S,T,Con) :-
  2529.       counter(cached_constrained_term_order_count,N),
  2530.       cache_size(CacheSize),
  2531.       (N > CacheSize -> (
  2532.         write_line(0,'*** cleared cached_constrained_term_order: ',N),
  2533.         set_counter(cached_constrained_term_order_count,0),
  2534.         abolish(cached_constrained_term_order,5),
  2535.         abolish(cached_constrained_term_order_complete,4)
  2536.       ); true),
  2537.       vars_tail({S,T},V),
  2538.       term_size_structure(S,SSize),
  2539.       term_size_structure(T,TSize),
  2540.       order_term_lpo_with_constraint_1(S,T,Con,V,SSize,TSize).
  2541.     order_term_lpo_with_constraint_1(S,T,Con,V,SSize,TSize) :-
  2542.       arg(1,SSize,Size1),
  2543.       arg(1,TSize,Size2),
  2544.       Size3 is (Size1*10000)+Size2,
  2545.       order_term_lpo_with_constraint_2(Size1,Size2,Size3,S,T,V,Con,SSize,
  2546.         TSize).
  2547.     order_term_lpo_with_constraint_2(_,_,Size3,S,T,V,Con,_,_) :-
  2548.       cached_constrained_term_order(Size3,S,T,var(V1,V2,V3),Con),
  2549.       not(not(const_list(V))),
  2550.       not(not(const_list(V3))),
  2551.       not(not((V1=V2,const_list(V)))).
  2552.     order_term_lpo_with_constraint_2(Size1,Size2,Size3,S,T,V,Con,SSize,
  2553.         TSize) :-
  2554.       not((
  2555.         const_list(V),
  2556.         cached_constrained_term_order_complete(Size3,S,T,V)
  2557.       )),
  2558.       order_term_lpo_with_constraint_3(S,T,Con,V,SSize,TSize),
  2559.       not((
  2560.         const_list(V),
  2561.         cached_constrained_term_order(Size3,S,T,var(V1,V1,V),Con)
  2562.       )),
  2563.       term_size(Con,Size4),
  2564.       linearize_term({S,T},{S1,T1},V1,V2),
  2565.       Size5 is Size1+Size2+Size4,
  2566.       increment_counter(cached_constrained_term_order_count,Size5),
  2567.       assert(cached_constrained_term_order(Size3,S1,T1,var(V1,V2,V),Con)).
  2568.     order_term_lpo_with_constraint_2(Size1,Size2,Size3,S,T,V,_,_,_) :-
  2569.       !,
  2570.       not((
  2571.         const_list(V),
  2572.         cached_constrained_term_order_complete(Size3,S,T,V)
  2573.       )),
  2574.       Size4 is Size1+Size2,
  2575.       increment_counter(cached_constrained_term_order_count,Size4),
  2576.       assert(cached_constrained_term_order_complete(Size3,S,T,V)),
  2577.       fail.
  2578.     order_term_lpo_with_constraint_3(S,T,Con,V,SSize,TSize) :-
  2579.       order_term_lpo(S,O,T),
  2580.       ((O=='<'; O=='=') -> fail; (
  2581.         (O=='>' -> (Con=[]); (
  2582.           ((var(S); var(T)) -> (Con=[S>T]); (
  2583.             (
  2584.               functor(S,_,N),
  2585.               order_term_lpo_with_constraint_4(N,S,T,Con,V,SSize,TSize)
  2586.             ); (
  2587.               functor(S,Sf,Sn),
  2588.               functor(T,Tf,Tn),
  2589.               (lex_eq(Sf,Sn,Tf,Tn) -> (
  2590.                 order_term_lpo_with_constraint_5(Sn,S,T,[],Con,V,SSize,TSize)
  2591.               ); (
  2592.                 (lex_gt(Sf,Sn,Tf,Tn) -> (
  2593.                   order_term_lpo_with_constraint_7(Tn,S,T,[],Con,V,SSize,TSize)
  2594.                 ); fail)
  2595.               ))
  2596.             )
  2597.           ))
  2598.         ))
  2599.       )).
  2600.     order_term_lpo_with_constraint_4(N,S,T,Con,V,SSize,TSize) :-
  2601.       enumerate(1,N,1,I),
  2602.       arg(I,S,Si),
  2603.       I1 is I+1,
  2604.       arg(I1,SSize,SiSize),
  2605.       order_term_lpo_with_constraint_1(Si,T,Con,V,SiSize,TSize).
  2606.     order_term_lpo_with_constraint_5(0,_,_,_,_,_,_,_) :-
  2607.       !,
  2608.       fail.
  2609.     order_term_lpo_with_constraint_5(N,S,T,Con1,Con2,V,SSize,TSize) :-
  2610.       functor(S,_,N1),
  2611.       N2 is N1-N+1,
  2612.       arg(N2,S,Si),
  2613.       arg(N2,T,Ti),
  2614.       equal_term_lpo_with_constraint(Si,Ti,Con3),
  2615.       append(Con1,Con3,Con4),
  2616.       identical_delete_all_duplicates(Con4,Con5),
  2617.       constraint_consistent(Con5),
  2618.       N3 is N-1,
  2619.       order_term_lpo_with_constraint_5(N3,S,T,Con5,Con2,V,SSize,TSize).
  2620.     order_term_lpo_with_constraint_5(N,S,T,Con1,Con2,V,SSize,TSize) :-
  2621.       functor(S,_,N1),
  2622.       N2 is N1-N+1,
  2623.       arg(N2,S,Si),
  2624.       arg(N2,T,Ti),
  2625.       N3 is N2+1,
  2626.       arg(N3,SSize,SiSize),
  2627.       arg(N3,TSize,TiSize),
  2628.       order_term_lpo_with_constraint_1(Si,Ti,Con3,V,SiSize,TiSize),
  2629.       append(Con1,Con3,Con4),
  2630.       identical_delete_all_duplicates(Con4,Con5),
  2631.       constraint_consistent(Con5),
  2632.       N4 is N-1,
  2633.       order_term_lpo_with_constraint_6(N4,S,T,Con5,Con2,V,SSize,TSize).
  2634.     order_term_lpo_with_constraint_6(0,_,_,Con,Con,_,_,_) :- !.
  2635.     order_term_lpo_with_constraint_6(N,S,T,Con1,Con2,V,SSize,TSize) :-
  2636.       functor(T,_,N1),
  2637.       N2 is N1-N+1,
  2638.       arg(N2,T,Ti),
  2639.       N3 is N2+1,
  2640.       arg(N3,TSize,TiSize),
  2641.       order_term_lpo_with_constraint_1(S,Ti,Con3,V,SSize,TiSize),
  2642.       append(Con1,Con3,Con4),
  2643.       identical_delete_all_duplicates(Con4,Con5),
  2644.       constraint_consistent(Con5),
  2645.       N4 is N-1,
  2646.       order_term_lpo_with_constraint_6(N4,S,T,Con5,Con2,V,SSize,TSize).
  2647.     order_term_lpo_with_constraint_7(0,_,_,Con,Con,_,_,_).
  2648.     order_term_lpo_with_constraint_7(N,S,T,Con1,Con2,V,SSize,TSize) :-
  2649.       functor(T,_,N1),
  2650.       N2 is N1-N+1,
  2651.       arg(N2,T,Ti),
  2652.       N3 is N2+1,
  2653.       arg(N3,TSize,TiSize),
  2654.       order_term_lpo_with_constraint_1(S,Ti,Con3,V,SSize,TiSize),
  2655.       append(Con1,Con3,Con4),
  2656.       identical_delete_all_duplicates(Con4,Con5),
  2657.       constraint_consistent(Con5),
  2658.       N4 is N-1,
  2659.       order_term_lpo_with_constraint_7(N4,S,T,Con5,Con2,V,SSize,TSize).
  2660.  
  2661. %%%
  2662. %%% Equal_term_lpo_with_constraint(S,T,Con)
  2663. %%%   input
  2664. %%%     S   : term
  2665. %%%     T   : term
  2666. %%%   output
  2667. %%%     Con : constraint consistent with respect to lpo under which S = T
  2668. %%%   exceptions
  2669. %%%     fails if not S > T under any constraint consistent with respect to lpo
  2670. %%%   notes
  2671. %%%     additional constraints consistent with respect to lpo under which
  2672. %%%       S = T are returned on backtracking
  2673. %%%
  2674.     equal_term_lpo_with_constraint(S,T,Con) :-
  2675.       order_term_lpo(S,O,T),
  2676.       ((O=='<'; O=='>') -> fail; (
  2677.         (O=='=' -> (Con=[]); (
  2678.           ((var(S); var(T)) -> (
  2679.             fail
  2680.           ); (
  2681.             functor(S,Sf,Sn),
  2682.             functor(T,Tf,Tn),
  2683.             (lex_eq(Sf,Sn,Tf,Tn) -> (
  2684.               equal_term_lpo_with_constraint_1(Sn,S,T,[],Con)
  2685.             ); fail)
  2686.           ))
  2687.         ))
  2688.       )).
  2689.     equal_term_lpo_with_constraint_1(0,_,_,Con,Con) :- !.
  2690.     equal_term_lpo_with_constraint_1(N,S,T,Con1,Con2) :-
  2691.       functor(S,_,N1),
  2692.       N2 is N1-N+1,
  2693.       arg(N2,S,Si),
  2694.       arg(N2,T,Ti),
  2695.       equal_term_lpo_with_constraint(Si,Ti,Con3),
  2696.       append(Con1,Con3,Con4),
  2697.       identical_delete_all_duplicates(Con4,Con5),
  2698.       constraint_consistent(Con5),
  2699.       N3 is N-1,
  2700.       equal_term_lpo_with_constraint_1(N3,S,T,Con5,Con2).
  2701.  
  2702. %%%
  2703. %%% rewrite_replace_rules
  2704. %%%
  2705.     rewrite_replace_rules :-
  2706.       bagof1([R,V,F,C],W^replace_rule_1(R,V,W,F,C),Rules_1),
  2707.       rewrite_replace_rules_1(Rules_1),
  2708.       bagof1([R,V,F,C],W^replace_rule_2(R,V,W,F,C),Rules_2),
  2709.       rewrite_replace_rules_5(Rules_2).
  2710.     rewrite_replace_rules_1([]) :- !.
  2711.     rewrite_replace_rules_1([[R,V,F,C]|Rules]) :-
  2712.       (constrained_rewriting -> (
  2713.         (restricted_rewrite -> (
  2714.           abolish(restricted_rewrite,0),
  2715.           rewrite_clause_with_constraint(R,_,_,[],Cts),
  2716.           assert(restricted_rewrite)
  2717.         ); (
  2718.           rewrite_clause_with_constraint(R,_,_,[],Cts)
  2719.         )),
  2720.         rewrite_replace_rules_2(Cts,Rs1),
  2721.         identical_delete_all_duplicates(Rs1,Rs2),
  2722.         identical_delete_all(R,Rs2,Rs3),
  2723.         (Rs2==Rs3 -> (
  2724.           not(not(rewrite_replace_rules_3(R,V)))
  2725.         ); true),
  2726.         rewrite_replace_rules_4(Rs3,F,C)
  2727.       ); (
  2728.         (restricted_rewrite -> (
  2729.           abolish(restricted_rewrite,0),
  2730.           rewrite_clause(R,_,_,0,R1,_),
  2731.           assert(restricted_rewrite)
  2732.         ); (
  2733.           rewrite_clause(R,_,_,0,R1,_)
  2734.         )),
  2735.         (R\==R1 -> (
  2736.           not(not(rewrite_replace_rules_3(R,V))),
  2737.           vars_tail(R1,V1),
  2738.           (not(duplicate_repeated_replace_rule(R1,V1)) -> (
  2739.             vars_literals(R1,W1),
  2740.             assert(replace_rule_1(R1,V1,W1,F,C))
  2741.           ); true)
  2742.         ); true)
  2743.       )),
  2744.       rewrite_replace_rules_1(Rules).
  2745.     rewrite_replace_rules_2([],[]) :- !.
  2746.     rewrite_replace_rules_2([[R,_,_]|Cts],[R|Rs]) :-
  2747.       rewrite_replace_rules_2(Cts,Rs).
  2748.     rewrite_replace_rules_3(R,V) :-
  2749.       const_list(V),
  2750.       retract(replace_rule_1(R,V,_,_,_)),
  2751.       !.
  2752.     rewrite_replace_rules_4([],_,_) :- !.
  2753.     rewrite_replace_rules_4([R|Rs],F,C) :-
  2754.       vars_tail(R,V),
  2755.       (not(duplicate_repeated_replace_rule(R,V)) -> (
  2756.         vars_literals(R,W),
  2757.         assert(replace_rule_1(R,V,W,F,C))
  2758.       ); true),
  2759.       rewrite_replace_rules_4(Rs,F,C).
  2760.     rewrite_replace_rules_5([]) :- !.
  2761.     rewrite_replace_rules_5([[R,V,F,C]|Rules]) :-
  2762.       (constrained_rewriting -> (
  2763.         (restricted_rewrite -> (
  2764.           abolish(restricted_rewrite,0),
  2765.           rewrite_clause_with_constraint(R,_,_,[],Cts),
  2766.           assert(restricted_rewrite)
  2767.         ); (
  2768.           rewrite_clause_with_constraint(R,_,_,[],Cts)
  2769.         )),
  2770.         rewrite_replace_rules_2(Cts,Rs1),
  2771.         identical_delete_all_duplicates(Rs1,Rs2),
  2772.         identical_delete_all(R,Rs2,Rs3),
  2773.         (Rs2==Rs3 -> (
  2774.           not(not(rewrite_replace_rules_6(R,V)))
  2775.         ); true),
  2776.         rewrite_replace_rules_7(Rs3,F,C)
  2777.       ); (
  2778.         (restricted_rewrite -> (
  2779.           abolish(restricted_rewrite,0),
  2780.           rewrite_clause_np(R,_,_,0,R1,_),
  2781.           assert(restricted_rewrite)
  2782.         ); (
  2783.           rewrite_clause_np(R,_,_,0,R1,_)
  2784.         )),
  2785.         (R\==R1 -> (
  2786.           not(not(rewrite_replace_rules_6(R,V))),
  2787.           vars_tail(R1,V1),
  2788.           (not(duplicate_once_replace_rule(R1,V1)) -> (
  2789.             vars_literals(R1,W1),
  2790.             assert(replace_rule_2(R1,V1,W1,F,C))
  2791.           ); true)
  2792.         ); true)
  2793.       )),
  2794.       rewrite_replace_rules_5(Rules).
  2795.     rewrite_replace_rules_6(R,V) :-
  2796.       const_list(V),
  2797.       retract(replace_rule_2(R,V,_,_,_)),
  2798.       !.
  2799.     rewrite_replace_rules_7([],_,_) :- !.
  2800.     rewrite_replace_rules_7([R|Rs],F,C) :-
  2801.       vars_tail(R,V),
  2802.       (not(duplicate_once_replace_rule(R,V)) -> (
  2803.         vars_literals(R,W),
  2804.         assert(replace_rule_2(R,V,W,F,C))
  2805.       ); true),
  2806.       rewrite_replace_rules_7(Rs,F,C).
  2807.  
  2808. %%%
  2809. %%% Abolish_rewrite_caches.
  2810. %%%
  2811.     abolish_rewrite_caches :-
  2812.       abolish(cached_rewrite,5),
  2813.       set_counter(cached_rewrite_count,0),
  2814.       abolish(cached_subterm_rewrite,6),
  2815.       set_counter(cached_subterm_rewrite_count,0),
  2816.       abolish(cached_restricted_clause_rewrite,4),
  2817.       abolish(cached_nonrestricted_clause_rewrite,4),
  2818.       set_counter(cached_clause_rewrite_count,0).
  2819.